1

I am trying to attach a simple function to the Yoast SEO filter hook wpseo_sitemap_urlimages and get the function to run, but I cannot do so. My code is:

function tp_filter_wpseo_sitemap_urlimages($images, $post_id) {
    error_log("test message");
    return $images;
}
add_filter('wpseo_sitemap_urlimages', 'tp_filter_wpseo_sitemap_urlimages');

To get this function to run, I have tried opening the following Yoast XML sitemaps:

My eventual goal with this is to add additional images to the Yoast post and page sitemaps (per these Yoast instructions).

The only support Yoast has offered for this is to direct me to this page.

  • IIRC the sitemaps are cached in wp_options, that‘s why you might not immediately see anything on the frontend – swissspidy Oct 9 '18 at 6:07
  • @swissspidy Thanks for that. But I'm not asking why nothing appears on the front-end. But shouldn't the hook still fire? I'm asking why the hook isn't firing at all. – cag8f Oct 10 '18 at 8:58
2

What I wanted to say in my previous comment:

This hook is only fired when the sitemap is being built. After that, it's cached and always served from cache. That means you'll never see any change on the frontend because it's all cached.

To disable the cache you can use the following code during development:

add_filter( 'wpseo_enable_xml_sitemap_transient_caching', '__return_false' );

After that, the sitemap is never served from cache but always built when viewing the sitemap in the frontend. Then you'll see some changes.

  • OK thanks. That should work. Let me give that a try--I'll need a few days. In the meantime though, instead of disabling the cache, is there a way to tell Yoast to explicitly re-build the sitemap? – cag8f Oct 10 '18 at 13:23
  • OK thanks for that. I added the cache disabling code, re-loaded the post sitemap page, and then my hook fired as expected. For the future though, how did you know the sitemap was cached, and how did you know that the caching could be disabled? Is there any documentation that describes this behavior? Or was asking here my only recourse? – cag8f Oct 15 '18 at 12:08
  • I had to extend the sitemap once for a project and had the same struggle as you. Then I just looked at the code to see how things work :-) Haven't looked for docs, so I don't know if there's a way to force a re-build (besides deleting the entries from the database) as disabling the cache was enough for me. – swissspidy Oct 15 '18 at 15:07
  • Ah yes, looking at the code--hadn't thought about doing that, but I guess I should give that a shot if I'm out of options. Thanks for all. – cag8f Oct 16 '18 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.