Home Forums WoodMart support forum Code provided by your theme not working good anymore Reply To: Code provided by your theme not working good anymore

#271608

Jeriss Cloud Center
Participant

Dear,

Alright I found the plugin that is causing the provided code to not work properly.

> The issue appears when WP Rocket is enabled.
> The issue disappears when WP Rocket is disabled.

After testing, I found that it’s the “Delay JavaScript execution” that is the rootcause.

So I found where the problem is but now I still need your help to know which precise js script of your theme I should exclude from the Delay to allow the correct functionning of this code.

I don’t want to exclude all Woodmart theme js (for performance related reasons). I want to exclude only the script precisely related to this custom code. Can you help to identify which script I should exclude from being delayed?

Staging credentials in private.

Thanks for your help
Gevcen

add_action(
	'woodmart_after_body_open',
	function() {
		remove_action( 'woodmart_attachment', 'woodmart_lazy_attachment_replace', 10) ;
		add_filter('woodmart_attachment', 'woodmart_custom_lazy_attachment_replace', 10, 3);
	}
);
add_action(
	'woodmart_after_header',
	function() {
		add_filter('woodmart_attachment', 'woodmart_lazy_attachment_replace', 10, 3);
		remove_action( 'woodmart_attachment', 'woodmart_custom_lazy_attachment_replace', 10) ;
	}
);
function woodmart_custom_lazy_attachment_replace( $imgHTML, $attach_id, $size ) {
	if ( preg_match( "/src=['\"]data:image/is", $imgHTML ) ) return $imgHTML;
	if( $attach_id ) {
		$lazy_image = woodmart_get_attachment_placeholder( $attach_id, $size );
	} else {
		$lazy_image = woodmart_lazy_get_default_preview();
	}
	return  woodmart_custom_lazy_replace_image( $imgHTML, $lazy_image );
}
function woodmart_custom_lazy_replace_image( $html, $src ) {
	$class = woodmart_custom_lazy_css_class();
	$new = preg_replace( '/<img(.*?)src=/is', '<img$1src="'.$src.'" data-wood-src=', $html );
	$new = preg_replace( '/<img(.*?)srcset=/is', '<img$1srcset="" data-srcset=', $new );
	if ( preg_match( '/class=["\']/i', $new ) ) {
		$new = preg_replace( '/class=(["\'])(.*?)["\']/is', 'class=$1' . $class . ' $2$1', $new );
	} else {
		$new = preg_replace( '/<img/is', '<img class="' . $class . '"', $new );
	}
	return $new;
}
function woodmart_custom_lazy_css_class() {
	$class = 'woodmart-lazy-load-custom';
	$class .= ' woodmart-lazy-' . woodmart_get_opt( 'lazy_effect' );
	return $class;
}

and

jQuery('.whb-navigation li').hover(function() {
   customLazy(jQuery(this));
});
jQuery('.whb-navigation li').on('mousemove',function() {
   customLazy(jQuery(this));
});
function customLazy($this){
    $this.find('.woodmart-lazy-load-custom').each(function() {
        var $img = jQuery(this);
        $img.attr('src', jQuery(this).data('wood-src'));
        $img.addClass('woodmart-loaded');
        var $categories = $img.parents('.categories-masonry');
        if ($categories.length > 0) {
            $categories.imagesLoaded(function() {
                $categories.packery();
            });
        }
    });
}