CDN images in sitemap

#874926
  • Resolved Valerio
    Rank Math free

    Hi Rank Math Support Team,

    I have previously attempted to resolve an issue with my website’s sitemap but did not succeed. I believe Rank Math is an excellent plugin and would like to give it another try for my site.

    My setup involves storing all images on an external bucket (DigitalOcean) and serving them through a CDN (bunny.net). Unfortunately, only the featured image is included in the sitemap with the correct CDN URL, while other images within the posts are not included. I have tried implementing a snippet suggested by the Rank Math Facebook group, but it was unsuccessful. Here is the snippet I attempted: https://rankmath.com/kb/filters-hooks-api-developer/#remove-change-images

    Could you please provide any further suggestions or guidance on how I might resolve this issue?
    Thank you for your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    We apologize for the issue you are facing with your website’s sitemap. Thank you for reaching out and for your kind words about Rank Math.

    When it comes to including images in your sitemap, Rank Math typically includes only the featured image by default. To include additional images from your posts, you may need to ensure that the images are properly referenced in the content and that there are no restrictions preventing them from being indexed.

    Here are a few suggestions to help resolve the issue:

    1. Check Image URLs: Ensure that the URLs of the images within your posts are correctly pointing to the CDN. If they are not, Rank Math may not recognize them as valid images to include in the sitemap.

    2. Use the Correct Filter: The snippet you mentioned from the Rank Math documentation is intended to modify how images are handled in the sitemap. Make sure you are implementing it correctly in your theme’s rank-math.php file. Here’s a basic example of how to include all images:

      add_filter( 'rank_math/sitemap/images', function( $images, $post ) {
         // Add logic to include additional images here
         return $images;
      }, 10, 2 );

      You can customize this filter to include images from your CDN.

    3. Exclude Sitemap from Caching: If you are using caching plugins, ensure that your sitemap is excluded from caching. Cached sitemaps may not reflect the latest changes on your site. You can find more information on how to exclude sitemaps from caching here.

    4. Check for Plugin Conflicts: Sometimes, other plugins can conflict with Rank Math’s functionality. You can use the Health Check plugin to troubleshoot any potential conflicts.

    5. Submit Your Sitemap Again: After making these changes, make sure to resubmit your sitemap to Google Search Console to ensure that it picks up the latest version.

    If these suggestions do not resolve the issue, please reply to this ticket, and a human agent from the Rank Math Support team will be happy to assist you further.

    Thank you for your patience, and we hope this helps!

    Sources:
    Rank Math Sitemap Issues and Their Fixes » Rank Math

    Hello,

    Thank you for your query and we are so sorry about the trouble this must have caused.

    The filter code you mentioned only lets you filter out the incoming image paths to be added to the sitemap so it may not work in your current situation.

    Please make use of this filter code instead:

    add_filter( 'rank_math/sitemap/xml_img_src', function( $src, $post ){
    	$src = str_replace( 'http://domain.com', 'https://cdn.domain.com', $src );
    	return $src;
    }, 10, 2);

    If the issue persists, we would like to check this further. Your WordPress setup doesn’t allow access to your theme file so we can’t check the code you added.

    Can you please share an example page where it has CDN images that should be added to the sitemap but its currently not?

    Looking forward to helping you.

    Valerio
    Rank Math free

    Hi Jeremy,

    I’ve tried the code you sent, but unfortunately, it still doesn’t work.

    Here is a sample post where all images are served from the CDN, but Rank Math isn’t detecting them (all images on the website are served via CDN):
    https://stage5.nowfashion.com/masha-ma-ready-to-wear-fall-winter-2019-paris/

    This post is also listed at the top of this sitemap page:
    https://stage5.nowfashion.com/post-sitemap17.xml

    All posts are merked that they have just one image (the featured one) but not all the others included.

    I’ve granted you admin privileges, so you should now have access to the theme files.

    Looking forward to your assistance.

    Hello,

    We attempted to access your site but were unsuccessful:
    https://imgur.com/HHlHc5F

    Since you’re serving images from an external CDN, Rank Math needs to recognize these CDN-hosted images when generating the sitemap.

    Please try this updated filter:

    add_filter( 'rank_math/sitemap/xml_img_src', function( $src, $post ){
        // Replace the domain to ensure the correct CDN URL is used for all images
        $src = str_replace( 'https://stage5.nowfashion.com', 'https://cdn.yourcdn.com', $src );
        return $src;
    }, 10, 2 );

    Please ensure that the image paths and CDN URLs in the filter above are consistent with the ones in your setup.

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Valerio
    Rank Math free

    Good morning, and thank you again for your support.

    I’ve tried again but still haven’t had any luck. After extensive testing, I discovered that the issue is related to the plugin “WP Offload Lite.” When I disable this plugin, all the images appear correctly in the sitemap. However, without the plugin, my site is broken because the images are missing, they are stored externally.

    So, the problem isn’t that I’m using a CDN, but rather that the images are offloaded to an external bucket (DigitalOcean).

    Hello,

    Yes, if the images are off-loaded they cannot be detected by our plugin because we read the content of the posts/pages directly from the database to retrieve the data for the images.

    Unfortunately, at the moment, we cannot change the way we detect the images in the sitemap but if we can do something to improve this in the future and introduce compatibility with off-loading images we’ll let you know of the same.

    Thank you.

    I may have found a solution. I’ve implemented the following code, and now it includes the images from the external bucket in the sitemap. Could you guys take a look and share your thoughts? If it works, feel free to use it, it might help other users experiencing issues with WP Offload Media and Rank Math compatibility.

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
        if ( 'post' === $type ) {
            $post_id = $object->ID;
    
            // Set the arguments for get_posts
            $args = array(
                'post_type'              => 'attachment',
                'post_mime_type'         => 'image',
                'post_parent'            => $post_id,
                'posts_per_page'         => -1,
                'fields'                 => 'ids', // Get only the IDs to improve performance
                'no_found_rows'          => true,  // Do not count the total number of results
                'update_post_meta_cache' => false,
                'update_post_term_cache' => false,
            );
    
            $attachment_ids = get_posts( $args );
    
            if ( ! empty( $attachment_ids ) ) {
                foreach ( $attachment_ids as $attachment_id ) {
                    $image_url = wp_get_attachment_url( $attachment_id );
                    if ( $image_url ) {
                        $url['images'][] = array(
                            'src' => $image_url,
                        );
                    }
                }
            }
        }
        return $url;
    }, 10, 3 );
    

    Hello,

    Thank you so much for sharing this code with us and glad that this worked for you.

    You may observe if this code affects the performance of your sitemap. If not, then you can proceed with sharing this with Google.

    Hope that helps.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)

The ticket ‘CDN images in sitemap’ is closed to new replies.