Skip to content

Instantly share code, notes, and snippets.

@chriswiggins
Created August 17, 2018 05:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriswiggins/0756f61f745b9b08cc13e86573768f05 to your computer and use it in GitHub Desktop.
Save chriswiggins/0756f61f745b9b08cc13e86573768f05 to your computer and use it in GitHub Desktop.
Snippet to add images to Yoast sitemap from Impreza Wordpress theme
<?php
/* Add impreza images to Yoast sitemap, since Yoast doesn't read impreza's Page
* Builder code - borrowed from (https://github.com/Yoast/wordpress-seo/issues/4808) */
function impreza_filter_wpseo_sitemap_urlimages($images, $post_id)
{
$post = get_post($post_id);
if (is_object($post)) {
$content = $post->post_content;
# Parse impreza Image modules
preg_match_all("/image=\"(\d+)\"/", $content, $impreza_images);
if(count($impreza_images) < 2){
return $images;
}
foreach ($impreza_images[1] as $impreza_image_id)
{
$attachment = wp_prepare_attachment_for_js( $impreza_image_id );
$images[] = array(
'src' => $attachment["url"],
'title' => $attachment["title"],
'alt' => $attachment["description"]
);
}
}
return $images;
}
add_filter('wpseo_sitemap_urlimages', 'impreza_filter_wpseo_sitemap_urlimages', 10, 2);
?>
@xZeroMCPE
Copy link

-1 Add better error handling.

@chriswiggins
Copy link
Author

-1 Add better error handling.

@xZeroMCPE you're welcome to update it yourself if you think it's so terrible

@Const4ntFlux
Copy link

yoast sitemap generation and wpbakery broke again resulting in thin pages and seo penalty.
I'm trying to implement this snippet to manually add images for specific posts/pages/portfolios that contain images within wpbakery pagebuilder blocks.

Can you let me know how and where to put your snippet function to make it work? Do I have to create a function snippet like the above for each and every page/post/portfolio I'd like to manually inject into the sitemap, or can I just put every image and post in this function?
Where in this function do I have to put the page/post/image id to make it work?
Do I just replace url in "url" with my url?

        'src' => $attachment["url"],
        'title' => $attachment["title"],
        'alt' => $attachment["description"]

Or does this function work with ALL images on every post/page/portfolio out of the box?
Sorry for my ignorance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment