Support » Plugin: SEOPress, on-site SEO » Custom fields to image sitemap: only works if gallery is added to standard block

  • Resolved touto

    (@touto)


    Hi,

    I have added the code from here to my functions.php (for testing purposes I am using the theme Twenty Twenty-One). I add galleries via the Advanced Custom Fields WYSIWYG editor and would like the images to be indexed.

    However, it only works when there is an image or gallery in one of the standard blocks. Is this normal behaviour or have I missed something in the implementation? (Apologies if I have!)

    Also, I have a question regarding the structure of the sitemap. At the moment it looks like this for a post with a gallery (first image added in WP block in order to get the output):

    <url>
    <loc>xxx/2021/01/03/test-3-acf-gallery/</loc>
    <lastmod>2021-01-04T18:50:23+00:00</lastmod>
    <image:image>
    <image:loc><![CDATA[xxx/wp-content/uploads/2020/12/IMG_2615-843x1024.jpg]]></image:loc>
    </image:image>
    <image:image>
    <image:loc><![CDATA[<div id='gallery-1' class='gallery galleryid-58 gallery-columns-3 gallery-size-thumbnail'>
    <figure class='gallery-item'>
       <div class='gallery-icon landscape'>
    	<a href='xxx/gallery/img_2143/'><img width="150" height="150" src="xxx/wp-content/uploads/2020/12/IMG_2143-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="test" loading="lazy" style="width:100%;height:68.35%;max-width:2000px;" /></a>
       </div>
    </figure>
    <figure class='gallery-item'>
       <div class='gallery-icon landscape'>
    	<a href='xxx/gallery/img_2582/'><img width="150" height="150" src="xxx/wp-content/uploads/2020/12/IMG_2582-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="test" loading="lazy" style="width:100%;height:83.7%;max-width:2000px;" /></a>
       </div>
    </figure>
    </div>
    
    ]]></image:loc>
    </image:image>
    </url>

    Is this structure correct? I am not very familiar with SEO best practices but a brief look at the Google guidelines suggests that each image should be processed like this:

    <image:image>
          <image:loc>http://example.com/image.jpg</image:loc>
    </image:image>

    When I add a gallery via a Gutenberg block, that’s the structure I see in the sitemap.

    Many thanks in advance for any help

    • This topic was modified 1 month, 4 weeks ago by touto.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter touto

    (@touto)

    Regarding the structure of the sitemap for the custom fields galleries, I have adapted the code:

    add_filter('seopress_sitemaps_single_img','sp_sitemaps_single_img', 10, 2);
    function sp_sitemaps_single_img($imgs, $post_id){
    	//Example with Advanced Custom Fields plugin
    	//Replace get_field('test_img',$post_id) with your own custom field name
    	if (function_exists('get_field') && get_field('image', $post_id) !='') {
    
                //get each id from the gallery
    		$images = get_post_meta($post_id, 'image', true);
    		preg_match('/\[gallery.*ids=.(.*).\]/', $images, $ids);
    		$images_id = explode(",", $ids[1]);
    
                //loop through each id
    		foreach ($images_id as $image) {
    		$original = wp_get_attachment_image_src($image, $size = 'full', $icon = false);
    		$alt   = get_post_meta($image, '_wp_attachment_image_alt', true);
    		$html .= '<image:image>';
    		$html .= "\n";
    		$html .= '<image:loc>';
    		$html .= '<![CDATA['.$original[0].']]>';
    		$html .= '</image:loc>';
    		$html .= "\n";
    		$html .= '<image:caption>';
    		$html .= '<![CDATA['.$alt.']]>';
    		$html .= '</image:caption>';
    		$html .= "\n";
    		$html .= '</image:image>';
    		$html .= "\n";
    }
    		return $imgs.$html;
    }
    		else {
    			return $imgs;
    		}
    }
    

    Each gallery image is now shown like this in the sitemap:

    <image:image>
    <image:loc><![CDATA[xxx/wp-content/uploads/2021/01/img_5409.jpg]]></image:loc>
    <image:caption><![CDATA[test3]]></image:caption>
    </image:image>

    This looks better to me, although as mentioned in my original post, I’m not sure if it’s needed like this.

    The problem that I need to add an image/gallery to the standard WP block still persists though. So unless there is an image in the standard WP block, it doesn’t recognize the images in the ACF field.

    Plugin Author Benjamin Denis

    (@rainbowgeek)

    Hi,

    what’s the URL of your xml sitemaps please?

    Thank you!

    Thread Starter touto

    (@touto)

    Thanks for your reply! I’m not running the plugin on a live site at the moment, just a local server. I have done this on a new install of WordPress with just ACF and SEOPress installed (and using a standard theme, Twenty Twenty-One).

    I just added a WYSIWYG field to posts and inserted a gallery via Add Media -> Create Gallery in this field.

    I was testing different SEO plugins and yours works the best for me – the only thing that doesn’t seem to work is indexing the custom field galleries.

    Are you able to test this on your end or do I need to install it on a live site? Thanks

    Plugin Author Benjamin Denis

    (@rainbowgeek)

    Hi,

    I was able to test your code successfully.

    A suggestion is to add some checks before printing alt / title / image URL to avoid empty lines.

    You can find the official documentation here:

    https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps?hl=en

    Also, our next update will allow you to filter images without adding an image in the post content field for more flexibility.

    Thread Starter touto

    (@touto)

    Many thanks – from a first look, it seems to work (I put in the checks before printing alt/URL)!

    Plugin Author Benjamin Denis

    (@rainbowgeek)

    Super!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.