WordPress.org

Ready to get started?Download WordPress

Forums

WordPress SEO by Yoast
Offsite images in sitemaps. Google complains. (1 post)

  1. crudhunter
    Member
    Posted 1 year ago #

    WordPress SEO does not have an option to turn off images in sitemaps, which would be nice for people blocking image locations in robots.txt

    But more importantly, when extracting images from posts to be added to a sitemap, the plugin does not validate which domain they might be pointing to. (It does add the local domain prefix if no domain in the path).

    Since I in many cases use images that point off to my CDN (a different domain), this causes Google's webmaster tools to mark up sitemap warnings about bad/unreachable urls in the sitemaps. They complain about files being blocked by robots.txt, which is somewhat confusing, since it is not true. The real issue is the cross-domain image URLs, which is a violation of the sitemaps standard, unless the two domains are linked in their respective sitemaps.

    So.. FYI to anyone with that same problem. To circumvent this, I added a small filter function to kill off the sitemap images that are off-site.

    Until it gets potentially gets fixed, you can add this code to a choice of places. In my case I added it to my theme's functions.php file (a child theme).

    If you instead of just dropping off-site locations want to kill off all images in sitemaps, simply change the function to start with a line like 'return array();' . Returning an empty array for everything should effectively kill all images in sitemaps. Makes it look like no posts have images.

    // Yoast sitemaps contain images that are cross-site (off to CDNs). Google does not like it.
    function domain_kill_images($imagearr, $postid) {
        $myURL = get_bloginfo('url');
        $searchPattern = '#^' . $myURL . '#';
        do {
            $something_killed = false; // nothing killed yet
            foreach($imagearr as $key => $imageinfo) {
                if (!preg_match($searchPattern, $key)) {
                    // If this image is off-site, don't index it
                    unset($imagearr[$key]);
                    $something_killed = true; // Make outer loop run again
                    break; // Array counters killed by the unset. Restart deletions
                }
            }
        } while ($something_killed);
        return $imagearr;
    }
    
    // Filter out offsite images from wp_seo
    add_filter( 'wpseo_sitemap_urlimages', 'domain_kill_images', 10, 2);

    http://wordpress.org/extend/plugins/wordpress-seo/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

  • RSS feed for this topic
  • Started 1 year ago by crudhunter
  • This topic is not a support question
  • WordPress version: 3.3.2