Support » Plugin: Lazy Load » [Plugin: Lazy Load] Does not work with WP Category Posts List/Shortcodes

  • WP Category Posts List provides a shortcode to list posts from a specific category within a page. The lazy-load plugin causes thumbnails of those posts not to display.

    Lazy-load runs the following regex on all image tags:

    $content = preg_replace( '#<img([^>]+?)src=[\'"]([^\'">]*)[\'"]([^>]*)>#', sprintf( '<img${1}src="%s" data-lazy-src="${2}"${3}><noscript><img${1}src="${2}"${3}></noscript>', $placeholder_image ), $content );

    (in function add_image_placeholders( $content ), lazy-load.php.)

    This happens any time either the_content() or post_thumbnails_html() is called:

    add_filter( 'the_content', array( __CLASS__, 'add_image_placeholders' ), 11 );
    		add_filter( 'post_thumbnail_html', array( __CLASS__, 'add_image_placeholders' ), 11 );

    In the case of WP Category Posts List, this leads to the above regex replace being carried out twice – once on the post thumbnail in the plugin shortcode, and again for the page content that the shortcode output ends up in, creating incorrect HTML output.

    This can be fixed by adding additional checks so that post thumbnails are only regex replaced when the post_thumbnails_html() function is not being called from within a page or post.

    1. Edit function init:

    function init() {
    		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'add_scripts' ) );
    		add_filter( 'the_content', array( __CLASS__, 'add_image_placeholders' ), 11 );
    		add_filter( 'post_thumbnail_html', array( __CLASS__, 'add_image_placeholders_if_not_page' ), 11 );

    2. Add function add_image_placeholders_if_not_page:

    function add_image_placeholders_if_not_page( $content ) {
    		if( is_page() || is_post() )
    			return $content;
    		return self::add_image_placeholders( $content );

  • The topic ‘[Plugin: Lazy Load] Does not work with WP Category Posts List/Shortcodes’ is closed to new replies.