Support

Account

Home Forums Front-end Issues How to fix vanishing WYSIWYG content when using acf_the_content filter?

Helping

How to fix vanishing WYSIWYG content when using acf_the_content filter?

  • I’m using this function for lazy loading images with acf_the_content filter:

    function acf_content_image_lazy_load($the_content) {
    	$thecontent = get_the_content();
    		if(!empty($thecontent)) {
    			libxml_use_internal_errors(true);
    			$post = new DOMDocument();
    			//$post->loadHTML($the_content);
    			$post->loadHTML(mb_convert_encoding($the_content, 'HTML-ENTITIES', 'UTF-8'));
    			$imgs = $post->getElementsByTagName('img');
    			// Iterate each img tag
    			foreach( $imgs as $img ) {
    				if( $img->hasAttribute('data-src') ) continue;
    				$clone = $img->cloneNode();
    				$src = $img->getAttribute('src');
    				$img->removeAttribute('src');
    				$img->setAttribute('data-src', $src);
    				$srcset = $img->getAttribute('srcset');
    				$img->removeAttribute('srcset');
    				if( ! empty($srcset)) {
    					$img->setAttribute('data-srcset', $srcset);
    				}
    				$imgClass = $img->getAttribute('class');
    				$img->setAttribute('class', $imgClass . ' lazy');
    			};
    			return $post->saveHTML();
    		}
    	}
    add_filter('acf_the_content', 'acf_content_image_lazy_load', 15);

    but this code broke my all WYSIWYG fields, content from any field of this type is vanishing. If I use this: the_sub_field('text-block-editor', false, false); content is visible again, but no shortcode is working.

    How to handle with this?

  • This at the top of your function

    
    $thecontent = get_the_content();
    

    this is getting the main WP content value and replacing the value of the ACF field.

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

The topic ‘How to fix vanishing WYSIWYG content when using acf_the_content filter?’ is closed to new replies.