Support

Account

Home Forums General Issues Add filter to 'acf_the_content' for WP Gallery

Helping

Add filter to 'acf_the_content' for WP Gallery

  • Hi

    I want use lazy load script in my theme so I need add_filter to the_content and ACF Wyswig editor.

    So for filtering ‘acf_the_content’ i use this part of code:

    add_filter('acf_the_content', 'replace_img_src', 11);
    function replace_img_src($replace_img_src)
    {
        //-- Change src/srcset to data attributes.
        $replace_img_src = preg_replace("/<img(.*?)(src=|srcset=)(.*?)>/i", '<img$1data-$2$3>', $replace_img_src);
        //-- Add .lazyload class to each image that already has a class.
        $replace_img_src = preg_replace('/<img(.*?)class=\"(.*?)\"(.*?)>/i', '<img$1class="$2 lozad"$3>', $replace_img_src);
        //-- Add .lazyload class to each image that doesn't have a class.
        $replace_img_src = preg_replace('/<img(.*?)(?!\bclass\b)(.*?)/i', '<img$1 class="lozad"$2', $replace_img_src);
        return $replace_img_src;
    }

    but it’s works only for images inserted directly in field, not with WP Gallery. How to force this script to works with any images in Wysiwyg field?

    Thanks!

  • For sure, when I use the_content filter then this script works with WP Gallery without any problems.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Add filter to 'acf_the_content' for WP Gallery’ is closed to new replies.