Support

Account

Home Forums ACF PRO ACF + Yoast XML Sitemap Image Inclusion

Solving

ACF + Yoast XML Sitemap Image Inclusion

  • Hi all,

    I’ve been working on resolving this issue: Images added into an Advanced Custom Fields PRO image field are not showing in xml sitemap #6480

    TLDR; By default, the Yoast SEO plugin doesn’t pick up images for inclusion into XML sitemaps that have been added via ACF fields (even if you use something like ACF Content Analysis for Yoast SEO)

    After a bit of wrangling (i.e. trying out things that were way more complicated than necessary), I arrived at the solution below:

    function wpseo_xmlsitemap_add_attached_images( $images, $post_id ) { 
    	$attached_images = get_attached_media( 'image', $post_id);
    	if($attached_images){
    		foreach($attached_images as $attached_image){
    			$image_arr = array();
    			$image_arr['src'] = $attached_image->guid;
    			$images[] = $image_arr;
    		}
    	}
    	array_unique($images);
        return $images; 
    }; 
    
    add_filter( 'wpseo_sitemap_urlimages', 'wpseo_xmlsitemap_add_attached_images', 10, 2 ); 

    Before:
    before

    After:
    AFTER

    From my tests, I can confirm that this works, but would love to have some other people try it out and let me know if you can also replicate success.

    Thanks!

  • This worked for me! Thank you it was exactly what I needed.

  • This will only work if the media item was added to the library while on the particular post’s editor. I cooked up code to traverse the blocks, but it requires you to edit for each new block.

    Was wondering if anybody came across a “catch all” solution for adding images to the Yoast sitemap.

  • So I just wrote this routine which is more “hands off” when you’re adding blocks. You just register the block w/ this class and it should add images to the Yoast sitemap.

    
     class YoastSEOSitemapImages {
    
        public static $instance;
        private $registered_blocks;
        private $current_block;
    
        public function __construct() {
            
            $this->registered_blocks = [];
        
    
            add_filter( 'wpseo_sitemap_urlimages', [ $this, 'add_sitemap_images'], 10, 2 );
    
        }
    
        public function register_block_images( $block_name = '', $field_name = '', $field_type = 'image' ) {
    
            $this->registered_blocks[$block_name] = [
                'block_name' => $block_name,
                'field_name' => $field_name,
                'field_type' => $field_type,
            ];
            
    
        }
    
        public function is_registered_block( $block_name ) {
    
            $has_block = false;
    
            foreach ( $this->registered_blocks as $registered_block ) {
    
                if( $registered_block['block_name'] == $block_name ) {
                   
                   
    
                   $this->current_block = $registered_block;
    
                   $has_block = true;
    
                } 
                          
    
            }
    
            return $has_block;
    
        }
    
        
        public function add_sitemap_images( $images, $post_id ) {
    
          
    
            $post = get_post( $post_id );
            $images_to_add = [];
             
            if ( has_blocks( $post ) ) {
                
                $blocks = parse_blocks( $post->post_content );
                            
    
                foreach( $blocks as $block ) {
    
                    if ( $this->is_registered_block( $block['blockName'] ) ) {
    
                        //var_dump( $block );
    
                        switch ( $this->current_block['field_type'] ) { 
    
                            case 'gallery' :
                            
                                $image_ids = $block['attrs']['data'][$this->current_block['field_name']];
                            
                                foreach( $image_ids as $imid ) {
                                    $images_to_add[] = $imid;
                                }
    
                            break;
    
                            case 'repeater' :
                                
                                foreach( $block['attrs']['data'] as $key => $value ) {
    
                                   
    
                                    if ( strpos( $key, $this->current_block['field_name'] ) !== false ) {
    
                                        $images_to_add[] = $value;
    
                                    }
                                }
    
                            case 'image' :
                            default :
    
                                
    
                                //Now grab the data from the attrs
                                $images_to_add[] = $block['attrs']['data'][$this->current_block['field_name']];
    
                            break;
                        }
    
                    }
          
                }
    
            }
    
            foreach ( $images_to_add as $img_to_add ) {
    
                $images[] = [
                    'src' => wp_get_attachment_image_url( $img_to_add, 'large', false),
                    'title' => get_the_title( $img_to_add ),
                    'alt' => get_post_meta( $img_to_add, '_wp_attachment_image_alt', true )
                ];
    
            }
    
     
    
            return $images;
    
        }
    
        public static function get_instance() {
    
            if ( self::$instance == null ) {
                self::$instance = new self;
            }
    
            return self::$instance;
    
        }
    
     }
    
     function BricSitemapImages() {
    
        return YoastSEOSitemapImages::get_instance();
    
     }
    
    BricSitemapImages();
    

    Then you could register the block that has an image field like this:

        BricSitemapImages()->register_block_images( 'acf/pretty-rows', 'image', 'repeater' );
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘ACF + Yoast XML Sitemap Image Inclusion’ is closed to new replies.