How to replace SEO image URIs in XML sitemaps for CDN in WordPress

To replace WordPress SEO image URIs in XML sitemaps for CDN with functions.php, you can use the wpseo_xml_sitemap_img_src filter. This filter allows you to modify the image src attribute in the XML sitemap.

Here’s an example of how to use this filter:

// Replace SEO image URIs in XML sitemaps for CDN
// More snippets at wpunplugged.com 

function my_custom_xml_sitemap_img_src( $src, $img ) {
    // Replace the image src with your CDN URL
    $src = str_replace( home_url(), 'https://yourcdn.com', $src );
    return $src;
}
add_filter( 'wpseo_xml_sitemap_img_src', 'my_custom_xml_sitemap_img_src', 10, 2 );

In this example, the function my_custom_xml_sitemap_img_src() is hooked to the wpseo_xml_sitemap_img_src filter. This function takes two arguments: the original image src and an array of image information. The function then replaces the image src with your CDN URL using the str_replace() function, and returns the modified src.

You can add this code to your theme’s functions.php file or in a custom plugin.

Keep in mind that this filter is specific to the WordPress SEO plugin. If you are using a different plugin or theme for generating XML sitemaps, you may need to use a different filter or method to modify the image src attribute.

Share this post

You might also like...

Comment

Your email address will not be published. Required fields are marked *