Skip to content

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/
* Modified by: Team Yoast
* 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_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.

Copy link

@BN83 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.

Copy link

@Greenlanternfromhouston 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.

Copy link

@maustyle maustyle commented Jul 21, 2017

please fix this.

@baqarcodupio

This comment has been minimized.

Copy link

@baqarcodupio baqarcodupio commented Jul 21, 2017

Yes. This code is not working.

@amboutwe

This comment has been minimized.

Copy link
Owner Author

@amboutwe 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.

Copy link

@gdelwo 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.

Copy link
Owner Author

@amboutwe 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.

Copy link

@Maher-Daoudi 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.

Copy link

@Maher-Daoudi 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.

Copy link

@BVtester 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.

Copy link
Owner Author

@amboutwe 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.

Copy link

@janw-me 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');

@ghost

This comment has been minimized.

Copy link

@ghost ghost 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.

Copy link

@parsetty parsetty commented Aug 10, 2018

Thanks, Its working perfect cheers!!!!

@JakeLBeans

This comment has been minimized.

Copy link

@JakeLBeans JakeLBeans commented Jan 9, 2019

Would like to add that this is currently still working with WordPress on 5.0.2 and Yoast plugin on 9.4!

@jack-fdrv

This comment has been minimized.

Copy link

@jack-fdrv jack-fdrv commented Jan 9, 2019

I have 3 questions:

  1. Why we use this part?
    $wpseo_sitemaps->register_sitemap( 'TYPE', 'create_TYPE_sitemap' );

  2. How to add my own custom link to the sub xml page? Like to the "/page-sitemap.xml"

  3. How to add sub pages to the http://www.example.com/external-sitemap-1.xml

@AlexL-JWA

This comment has been minimized.

Copy link

@AlexL-JWA AlexL-JWA commented Sep 19, 2019

this code doesn’t work for me, I inserted it into function.php and it doesn’t show me my added sitemap
http://joxi.ru/l2ZLM6ETz1LEQr
http://joxi.ru/ZrJe4yGtwGDM4A

there are 18 of them and it remains to tell me what I'm doing wrong

@amboutwe

This comment has been minimized.

Copy link
Owner Author

@amboutwe amboutwe commented Sep 19, 2019

Please note that changes will be applied upon next sitemap update. To manually refresh the sitemap, please disable and enable the sitemaps.

If you have refreshed the sitemap, 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.

@leepowers

This comment has been minimized.

Copy link

@leepowers leepowers commented Feb 12, 2020

Two new gists as a more comprehensive examples. All in one file, including writing sitemap <url> tags with images:

Sitemap without a data source specified:
https://gist.github.com/leepowers/2b32f734571cbeadc811b93501cfcced

Sitemap sourced from post type(s):
https://gist.github.com/leepowers/946d9c72e06779acbb2a125e5b53f475

@ziamediagroup

This comment has been minimized.

Copy link

@ziamediagroup ziamediagroup commented Feb 28, 2020

Does anyone know how to REMOVE the line from the sitemap once you've added it? Even after refreshing the XML Sitemap option, and removing and reinstalling the plugin, that external sitemap is still showing

@amboutwe

This comment has been minimized.

Copy link
Owner Author

@amboutwe amboutwe commented Feb 29, 2020

Removing the code snippet will remove the custom functionality.

Additionally, this is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@Sarasranglt

This comment has been minimized.

Copy link

@Sarasranglt Sarasranglt commented May 1, 2020

Two new gists as a more comprehensive examples. All in one file, including writing sitemap <url> tags with images:

Sitemap without a data source specified:
https://gist.github.com/leepowers/2b32f734571cbeadc811b93501cfcced

Sitemap sourced from post type(s):
https://gist.github.com/leepowers/946d9c72e06779acbb2a125e5b53f475

@leepowers Thanks for this. This works.

@repl-tusar-rath

This comment has been minimized.

Copy link

@repl-tusar-rath repl-tusar-rath commented Nov 19, 2020

I have 3 questions:

  1. How to add my own custom link to the sub xml page? Like to the "/page-sitemap.xml"

@jack-fdrv
Did you ever got a solution for this?

@Paulsky

This comment has been minimized.

Copy link

@Paulsky Paulsky commented Jan 5, 2021

Two new gists as a more comprehensive examples. All in one file, including writing sitemap <url> tags with images:

Sitemap without a data source specified:
https://gist.github.com/leepowers/2b32f734571cbeadc811b93501cfcced

Sitemap sourced from post type(s):
https://gist.github.com/leepowers/946d9c72e06779acbb2a125e5b53f475

Thank you very much @leepowers!

@ROGMARQ14

This comment has been minimized.

Copy link

@ROGMARQ14 ROGMARQ14 commented Jun 10, 2021

I can´t find yoast_seo_sitemap_add_external.php anywhere on the plugin editor tab.

A little help here?

Also, I want the new custom sitemap I want to create to be inside of /sitemap_index.xml...Thoughts?

@amboutwe

This comment has been minimized.

Copy link
Owner Author

@amboutwe amboutwe commented Jun 10, 2021

I can´t find yoast_seo_sitemap_add_external.php anywhere on the plugin editor tab.

That filename is the one from the example above. It is not part of the plugin and you will not see custom sitemaps in the plugin settings or edit screens/tabs.

I want the new custom sitemap I want to create to be inside of /sitemap_index.xml

The code snippet will add a link to the sitemap_index.xml file with all the other sub-sitemaps.

@robwent

This comment has been minimized.

Copy link

@robwent robwent commented Jul 4, 2021

Is it possible to use this multiple times?

I'm using it in a custom plugin but also run another plugin that uses it.

It seems that using it multiple times just overwrites the filters from the other plugins and only the last run filter will be added.

@amboutwe

This comment has been minimized.

Copy link
Owner Author

@amboutwe amboutwe commented Jul 7, 2021

Yes, the code can be used multiple times. However, you will likely need to make changes to avoid overwriting. For example, changing the function name add_sitemap_custom_items to something unique. You can also add a priority level so one piece of code triggers before the other.

@robwent

This comment has been minimized.

Copy link

@robwent robwent commented Jul 7, 2021

I don't think it can be used multiple times?

Currently, another plugin is using the same filter to add a sitemap.

If I just use the same filter in my plugin then the sitemap only shows the filter from the first plugin.
if I increase the priority in my plugin then it overwrites the content added by the first plugin so my sitemap replaces the other one.

Both plugins have namespaced function names so I don't think that's the problem.

@amboutwe

This comment has been minimized.

Copy link
Owner Author

@amboutwe amboutwe commented Jul 8, 2021

Before I sent my last reply, I tested the filter to add external sitemaps and was able to add 4 sitemaps, 2 from a plugin file and 2 from a theme's functions.php.

As that isn't happening in your setup, the issue is likely specific to your setup and this is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@robwent

This comment has been minimized.

Copy link

@robwent robwent commented Jul 9, 2021

I found the problem.
The first plugin wasn't passing the $sitemap_custom_items variable into their custom function and just returning their own output.

So I had to up the priority in my own function and append my sitemap to theirs.

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