Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filters and example code for Yoast SEO sitemaps
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Create Custom Yoast Sitemap
* Credit: Unknown
* Last Tested: Unknown
*********
* HOW TO USE
* Replace TYPE with your custom type
*/
add_action( 'init', 'enable_custom_sitemap' );
function enable_custom_sitemap() {
global $wpseo_sitemaps;
if ( isset( $wpseo_sitemaps ) && ! empty ( $wpseo_sitemaps ) ) {
$wpseo_sitemaps->register_sitemap( 'TYPE', 'create_TYPE_sitemap' );
}
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Add External Sitemap to Yoast Sitemap Index
* Credit: Paul https://wordpress.org/support/users/paulmighty/
* Last Tested: Aug 25 2017 using Yoast SEO 5.3.2 on WordPress 4.8.1
*********
* This code adds two external sitemaps and must be modified before using.
* Replace http://www.example.com/external-sitemap-#.xml
with your external sitemap URL.
* Replace 2017-05-22T23:12:27+00:00
with the time and date your external sitemap was last updated.
Format: yyyy-MM-dd'T'HH:mm:ssZ
* If you have more/less sitemaps, add/remove the additional section.
*********
* Please note that changes will be applied upon next sitemap update.
* To manually refresh the sitemap, please disable and enable the sitemaps.
*/
add_filter( 'wpseo_sitemap_index', 'add_sitemap_custom_items' );
function add_sitemap_custom_items() {
$sitemap_custom_items = '
<sitemap>
<loc>http://www.example.com/external-sitemap-1.xml</loc>
<lastmod>2017-05-22T23:12:27+00:00</lastmod>
</sitemap>';
/* Add Additional Sitemap
* This section can be removed or reused as needed
*/
$sitemap_custom_items .= '
<sitemap>
<loc>http://www.example.com/external-sitemap-2.xml</loc>
<lastmod>2017-05-22T23:12:27+00:00</lastmod>
</sitemap>';
/* DO NOT REMOVE ANYTHING BELOW THIS LINE
* Send the information to Yoast SEO
*/
return $sitemap_custom_items;
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Change the number of entries per sitemap
* Credit: Yoast Team
* Last Tested: Oct 17 2018 using Yoast SEO 8.4 on WordPress 4.9.8
*********
*/
add_filter( 'wpseo_sitemap_entries_per_page', 'max_entries_per_sitemap' );
function max_entries_per_sitemap() {
return 10;
}
@BN83

This comment has been minimized.

BN83 commented Jun 27, 2017

Where should this go? I've put it in my 'wp-content/plugins/wordpress-seo' folder but the sitemap is not listing in my sitemap?

@Greenlanternfromhouston

This comment has been minimized.

Greenlanternfromhouston commented Jul 14, 2017

I've been copying and pasting this to my functions.php file with no success. I cannot figure out why it will not run and add additional sitemaps to sitemap_index.xml .

This does not work currently, Can we please get someone to fix it? Why doesn't it work? Yoast Version 4.8 even tried version Yoast 5.01

Author, will you please help us? Does this currently work for you in your theme's functions.php, did you add an add_action command or why is this not working for us?

@maustyle

This comment has been minimized.

maustyle commented Jul 21, 2017

please fix this.

@baqarcodupio

This comment has been minimized.

baqarcodupio commented Jul 21, 2017

Yes. This code is not working.

@amboutwe

This comment has been minimized.

Owner

amboutwe commented Jul 23, 2017

@BN83 @Greenlanternfromhouston @maustyle @baqarcodupio
The code has been confirmed as working when added to the functions.php file. However, the changes will be applied upon next sitemap update. To manually update the sitemap, please disable and enable the sitemap feature. This information has been added to the code.

Thanks!

@gdelwo

This comment has been minimized.

gdelwo commented Nov 1, 2017

I installed and my new sitemap is recognized, but I get the following error message in top of Yoast Dashboard.

The following file is blocking your XML sitemaps from working properly. Either delete it (this can be done with the "Fix it" button) or disable Yoast SEO XML sitemaps.
/public_html/X-sitemap.xml

Hope someone can help, thanks!

@amboutwe

This comment has been minimized.

Owner

amboutwe commented Nov 6, 2017

@gdelwo To receive free support for Yoast SEO we have the following channels:

Or if you want personal support via email, you can purchase Yoast SEO Premium.

Thanks for your understanding.

@Maher-Daoudi

This comment has been minimized.

Maher-Daoudi commented Jan 31, 2018

What can we add to make this code change the last update date for the external xml link to be more recent as the other xml link is updated?

@Maher-Daoudi

This comment has been minimized.

Maher-Daoudi commented Jan 31, 2018

and by the way - the code DOES work - you need to make sure you are adding it to the theme you are using.

IMPORTANT: In your dashboard click themes - then editor - then on the right side click functions file - you can scroll to the very bottom and add the below code and it will work:

you can just change your external siemap form what i have to yours - chage this one line (and the date)
https://eyelashr.com/sitemap-eyelash-extensions.xml

  • make sure to use http or https properly on your external sitemap.

You may be adding the code to the wrong functions file if it's not working for you - the below code is what I put in the functions code of theme we are using for https://eyelashr.com - We're the first "Uber for eyelash extensions" platform - if you're searching for eyelash extensions near me - check out htps://eyelashr.com

Here's the exact code we put and if you go to https://eyelashr.com/sitemap_index.xml - you'll see the last link as the external link

/* Add External Sitemap to Yoast Sitemap Index
 * Credit: Paul https://wordpress.org/support/users/paulmighty/
 * Last Tested: Aug 25 2017 using Yoast SEO 5.3.2 on WordPress 4.8.1
 *********
 * This code adds two external sitemaps and must be modified before using.
 * Replace http://www.example.com/external-sitemap-#.xml
   with your external sitemap URL.
 * Replace 2018-01-30T23:12:27+00:00
   with the time and date your external sitemap was last updated.
   Format: yyyy-MM-dd'T'HH:mm:ssZ
 * If you have more/less sitemaps, add/remove the additional section.
 *********
 * Please note that changes will be applied upon next sitemap update.
 * To manually refresh the sitemap, please disable and enable the sitemaps.
 */
add_filter( 'wpseo_sitemap_index', 'add_sitemap_custom_items' );
function add_sitemap_custom_items() {
   $sitemap_custom_items = '
<sitemap>
<loc>https://eyelashr.com/sitemap-eyelash-extensions.xml</loc>
<lastmod>2017-05-22T23:12:27+00:00</lastmod>
</sitemap>';
 
/* DO NOT REMOVE ANYTHING BELOW THIS LINE
 * Send the information to Yoast SEO
 */
return $sitemap_custom_items;
}
@BVtester

This comment has been minimized.

BVtester commented Feb 14, 2018

Hi dear,
I added successfully the code to have specific handmade posts to be taken into a custom sitemap. These posts have several images, but the sitemap mention 0 images. And indeed, the image:image tag is missing. How can we add it in a way that the sitemap automatically counts the number of images in the post?

@amboutwe

This comment has been minimized.

Owner

amboutwe commented Feb 15, 2018

@BVtester This is not the proper place to request support. Please check out our extensive Knowledge Base or visit the free support forum.

If you require further support, upgrading to our premium version provides you with access to our support team.

@janw-me

This comment has been minimized.

janw-me commented Feb 21, 2018

One thing to keep in mind, sitemaps are cached.

For development disable it using:

add_filter( 'wpseo_enable_xml_sitemap_transient_caching', '__return_false');

@blawz

This comment has been minimized.

blawz commented Mar 8, 2018

@janw-oostendorp nailed the issue for me. The code would not call my filter, but disabling the caching corrected the issue.
Thanks,
Bill

@parsetty

This comment has been minimized.

parsetty commented Aug 10, 2018

Thanks, Its working perfect cheers!!!!

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