` tags to WP core's XML sitemap (M56) — core doesn't
* emit these at all. Featured image only (deliberately simple, v1): scraping
* every `
` from post content risks pulling in unrelated images (ads,
* unrelated embeds, decorative icons rendered as `
` by some blocks) —
* the featured image is the one image a site owner has already, explicitly,
* chosen to represent the content.
*/
final class SitemapImageTags
{
public function register(): void
{
add_action( 'wp_sitemaps_init', array( $this, 'use_image_aware_renderer' ) );
add_filter( 'wp_sitemaps_posts_entry', array( $this, 'add_featured_image' ), 10, 2 );
}
public function use_image_aware_renderer( \WP_Sitemaps $wp_sitemaps ): void
{
/**
* Filters the renderer class WP core's sitemap system uses. Core
* only exposes ONE renderer slot (`$wp_sitemaps->renderer`, a public
* property with no accumulation mechanism) — a filter, not a second
* hard swap, is how a further extension (e.g. Pro's video sitemap,
* M56) can layer its OWN renderer on top of this one instead of the
* two silently fighting over the same property.
*
* @param string $class Fully-qualified class name, must extend ImageAwareSitemapRenderer.
*/
$class = (string) apply_filters( 'acez_seo_sitemap_renderer_class', ImageAwareSitemapRenderer::class );
$wp_sitemaps->renderer = new $class();
}
/**
* @param array $sitemap_entry
*
* @return array
*/
public function add_featured_image( array $sitemap_entry, \WP_Post $post ): array
{
if ( ! in_array( $post->post_type, PostTypes::supported(), true ) )
{
return $sitemap_entry;
}
$image_url = get_the_post_thumbnail_url( $post, 'full' );
if ( is_string( $image_url ) && '' !== $image_url )
{
$sitemap_entry['images'] = array( $image_url );
}
return $sitemap_entry;
}
}