Skip to main content

Sitemap

1. Filter to change the Sitemap URL Base

/**
* Filter to change the Sitemap URL Base
*
* @param string $base New URL Base
*/
add_filter( 'rank_math/sitemap/base_url', function( $base ){
return 'search-sitemaps/';
}, 10, 1 );

2. Filter to change the Sitemap Cache directory.

/**
* Filter XML sitemap cache directory.
*
* @param string $unsigned Default cache directory
*/
add_filter( 'rank_math/sitemap/cache_directory', function( $directory) {
return $directory;
});

3. Filter to enable/disable Sitemap caching.

/**
* Filter if XML sitemap transient cache is enabled.
*
* @param boolean $unsigned Enable cache or not, defaults to true
*/
add_filter( 'rank_math/sitemap/enable_caching', '__return_true');

4. Filter to change Sitemap caching mode.

/**
* Filter to change sitemap caching mode
*
* @param string $mode Cache mode "file" or "db".
*/
add_filter( 'rank_math/sitemap/cache_mode', function( $mode ) {
return $mode;
} );

5. Filter to exclude post type from the Sitemap.

This filter will override the Include in Sitemap option available under Rank Math SEO → Sitemap Settings → Post Types.

/**
* Filter decision if post type is excluded from the XML sitemap.
*
* @param bool $exclude Default false.
* @param string $type Post type name.
*/
add_filter( 'rank_math/sitemap/exclude_post_type', function( $exclude, $type ){
return $exclude;
}, 10, 2 );

6. Filter sitemap item before it gets added to the sitemap.

/**
* Filter URL entry before it gets added to the sitemap.
*
* @param array $url Array of URL parts.
* @param string $type URL type. Can be user, post or term.
* @param object $object Data object for the URL.
*/
add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
return $url;
}, 10, 3 );

7. Filter to change/remove archive url, plugin uses in the XML Sitemap.

/**
* Filter the URL Rank Math SEO uses in the XML sitemap for this post type archive.
*
* @param string $archive_url The URL of this archive
* @param string $post_type The post type this archive is for.
*/
add_filter( 'rank_math/sitemap/post_type_archive_link', function( $archive_url, $post_type ){
return $archive_url;
}, 10, 2 );

8. Filter to change/remove Post URL, plugin uses in the sitemap.

/**
* Filter the URL Rank Math SEO uses in the XML sitemap.
*
* Note that only absolute local URLs are allowed as the check after this removes external URLs.
*
* @param string $url URL to use in the XML sitemap
* @param object $post Post object for the URL.
*/
add_filter( 'rank_math/sitemap/xml_post_url', function( $url, $post){
return $url;
}, 10, 2 );

9. Filter to exclude taxonomy from Sitemap.

This filter will override the Include in Sitemap option available under Rank Math SEO → Sitemap Settings → Taxonomies.

/**
* Filter decision if taxonomy is excluded from the XML sitemap.
*
* @param bool $exclude Default false.
* @param string $type Taxonomy name.
*/
add_filter( 'rank_math/sitemap/exclude_taxonomy', function( $exclude, $type ){
return $exclude;
}, 10, 2 );

10. Filter to exclude empty terms from Sitemap.

/**
* Filter the setting of excluding empty terms from the XML sitemap.
*
* @param boolean $exclude Defaults to true.
* @param array $taxonomy_names Array of names for the taxonomies being processed.
*/
add_filter( 'rank_math/sitemap/exclude_empty_terms', function( $exclude , $taxonomy_names ){
return $exclude;
}, 10, 2 );

11. Filter to remove/change images included for the post in XML sitemap.

/**
* Filter images to be included for the post in XML sitemap.
*
* @param array $images Array of image items.
* @param int $post_id ID of the post.
*/
add_filter( 'rank_math/sitemap/urlimages', function( $images, $post_id ){
return $images;
}, 10, 2);

12. Filter to Remove Sitemap credit.

/**
* Filter to remove sitemap credit.
*
* @param boolean Defaults to false.
*/
add_filter( 'rank_math/sitemap/remove_credit', '__return_true');

13. Filter to Add Extra URLs in the Sitemap

/**
Filter to add extra URLs to the XML sitemap by type.
*
Only runs for the first page, not on all.
*
@param string $content String content to add, defaults to empty.
*/
add_action( 'rank_math/sitemap/{$type}_content', function() {
return '<url>
<loc>https://rankmath.com/some-custom-url/</loc>
<lastmod>2020-06-10T20:20:20+00:00</lastmod>
</url>';
});

Replace {$type} with either post or page depending on if you want the URL to be included in the post or page sitemap – respectively.

14. Filter to add Locations KML file in the Sitemap

/**
* Filter to add Locations KML file in the sitemap
*/
add_filter( 'rank_math/sitemap/locations', '__return_true' );

15. Filter to Add CDN Image URLs in the Sitemap

/**
* Filter to Add CDN Image URLs in the Sitemap
*
* @param string $src Image URL.
* @param object $post Post object.
*/
add_filter( 'rank_math/sitemap/xml_img_src', function( $src, $post ){
$src = str_replace( 'http://domain.com', 'https://cdn.domain.com', $src );
return $src;
}, 10, 2);

16. Filter to detect third party sitemap URL in the Search Console

/**
* Filter to detect Sitemap URL in the Search Console if you are using different plugin for sitemap.
*/
add_filter( 'rank_math/sitemap/sitemap_index_uri', function() { return 'sitemap.xml'; } );

17. Filter to change user query arguments for the sitemap listing

/**
* Filter to change user query arguments for the sitemap listing
*/
add_filter( 'rank_math/sitemap/author/query', function( $args ) {
$args['role__not_in'] = 'subscriber'; return $args;
} );

18. Filter to add `noindex` URLs in the Sitemap

/**
* Filter to add `noindex` URLs in the Sitemap
*
* @param bool $value Whether to include noindex terms in Sitemap.
* @param string $type Object Type.
*
* @return boolean
*/
add_filter( 'rank_math/sitemap/include_noindex', function( $value, $type ) {
return true;
}, 10, 2 );

19. Filter to add external Sitemap to the Rank Math Sitemap

add_filter( 'rank_math/sitemap/index', function( $xml ) {
$xml .= '
<sitemap>
<loc>http://example.com/new-sitemap.xml</loc>
<lastmod>2020-09-14T20:34:15+00:00</lastmod>
</sitemap>';
return $xml;
}, 11 );

20. Filter to add content in the new sitemap

/**
* Filter to add links in the new Sitemap.
*/
add_filter( 'rank_math/sitemap/{$type}/content', function() {
return '
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://example.com/new-sitemap-post/</loc>
<lastmod>2020-09-14T20:34:15+00:00</lastmod>
</url>
</urlset>';
} );

21. Filter the sitemap HTTP headers

/**
* Filter the sitemap HTTP headers.
*
* @param array $headers HTTP headers.
* @param bool $is_xsl Whether these headers are for XSL.
*/
add_filter( 'rank_math/sitemap/http_headers', function( $headers ) {
if ( '/locations.kml' !== $_SERVER['REQUEST_URI'] ) {
return $headers;
}
unset( $headers['X-Robots-Tag'] );
return $headers;
} );

22. Change the post content before passing it to the Image Parser

/**
* Filter: 'rank_math/sitemap/content_before_parse_html_images' - Filters the post content
* before it is parsed for images.
*
* @param string $content The raw/unprocessed post content.
* @param int $post_id The current Post ID.
*/
add_filter( 'rank_math/sitemap/content_before_parse_html_images', function( $content, $post_id ) {
// Process content..
return $content;
}, 10, 2 );

23. Filter to change the sort order in the HTML Sitemap

/**
* Filter: 'rank_math/sitemap/html_sitemap/sort_items' - Allow changing the sort order of the HTML sitemap.
*
* @var array $sort {
* @type string $field The field to sort by.
* @type string $order The sort order.
* }
* @var string $type The item type.
* @var string $name The post type/taxonomy name.
*/
add_filter('rank_math/sitemap/html_sitemap/sort_items', function ($sort, $type, $name) {
return $sort;
}, 10, 3);

24. Filter to change the post statuses to be included in the HTML Sitemap

/**
* Filter: 'rank_math/sitemap/html_sitemap_post_statuses' - Allow changing the post statuses that should be included in the HTML sitemap.
*
* @var array $statuses Post statuses.
* @var string $post_type Post type name.
*/
add_filter('rank_math/sitemap/html_sitemap_post_statuses', function ($statuses, $post_type) {
return $statuses;
}, 10, 2);

25. Filter to change the post types to be included in the HTML Sitemap

/**
* Filter: 'rank_math/sitemap/html_sitemap_post_types' - Allow changing the post types to be included in the HTML sitemap.
*
* @var array $post_types The post types to be included in the HTML sitemap.
*/
add_filter('rank_math/sitemap/html_sitemap_post_types', function ($post_types) {
return $post_types;
}, 10, 1);

26. Filter to change the taxonomies to be included in the HTML Sitemap

/**
* Filter: 'rank_math/sitemap/html_sitemap_taxonomies' - Allow changing the taxonomies to be included in the HTML sitemap.
*
* @var array $taxonomies The taxonomies to be included in the HTML sitemap.
*/
add_filter('rank_math/sitemap/html_sitemap_taxonomies', function ($taxonomies) {
return $taxonomies;
}, 10, 1);

27. Filter to remove the HTML Sitemap tab from Sitemap Settings

/**
* Filter to remove HTML sitemap tab from the Sitemap settings.
*
* @param array $tabs
*/
add_filter( 'rank_math/settings/sitemap', function( $tabs) {
if ( isset( $tabs['html_sitemap'] ) ) {
unset( $tabs['html_sitemap'] );
}

return $tabs;
});

28. Filter to change video content and thumbnail location

In the below filter, replace {$prop} with thumbnail_loc to change the thumbnail location and content_loc to change the content location

/**
* Filter the video content and thumbnail location:
* - rank_math/sitemap/video/thumbnail_loc
* - rank_math/sitemap/video/content_loc
*/
add_filter("rank_math/sitemap/video/{$prop}", function ($loc) {
return $loc;
});

29. Filter to remove the Last Modified Column from Sitemap Index

/**
* Filter to remove lastmod from sitemap index
*/
add_filter( 'rank_math/sitemap/index/entry', function( $index, $type ) {
if ( isset( $index['lastmod'] ) ) {
unset( $index['lastmod'] );
}
return $index;
}, 10, 2 );

To remove the Last Modified columns from specific sitemaps in the index, the filter can be used as

/**
* Filter to remove lastmod from sitemap
*/
add_filter( 'rank_math/sitemap/index/entry', function( $index, $type ) {
if ( 'term' === $type ) {
return $index;
}

if ( isset( $index['lastmod'] ) ) {
unset( $index['lastmod'] );
}
return $index;
}, 10, 2 );

Note: Please be sure to flush the sitemap cache as shown in this tutorial for these changes to reflect on the front end.

30. Filter to modify the post object added to the Sitemap

/**
* Filter the post object before it gets added to the sitemap.
* This allows you to add custom properties to the post object,
* or replace it entirely.
*
* @param object $post Post object.
*/
add_filter('rank_math/sitemap/post_object', function($post){
return $post;
});

31. Filter to modify the Sitemap Index slug

/**
* Filter: 'rank_math/sitemap/index_slug' - Modify the sitemap index slug.
*
* @param string $slug Sitemap index slug.
*
* @return string
*/
add_filter( 'rank_math/sitemap/index/slug', function($slug) {
return $slug;
});

Note: If you have updated the Sitemap Index slug using the above filter, then head over to Settings → Permalinks in your WordPress dashboard and click the Save Changes button for the changes to reflect.

32. Filter to change Sitemap Slug

/**
* Filter the slug of the author sitemap.
*
* @param string $slug Slug of the author sitemap.
*/
add_filter( 'rank_math/sitemap/author/slug', function($slug){
return 'author'; //Change this to your preferred slug.
});

Similarly, the bellow filters can be used for changing the slug of video sitemap and news sitemap.

add_filter( 'rank_math/sitemap/video/slug', function( $slug ) {
return 'video'; //Change this slug.
});

add_filter( 'rank_math/sitemap/news/slug', function( $slug ) {
return 'news'; //Change this slug.
});

33. Filter to modify the value of <news:language> in News Sitemap

/**
* Filter: 'rank_math/sitemap/news/language' - Allow changing the news language based on the entity.
*
* @param string $lang Language code.
* @param array $entity Array of parts that make up this entry.
*/
add_filter( 'rank_math/sitemap/news/language', function( $lang, $entity ) {
return $lang;
}, 10, 2 );

34. Filter to change the content passed to the Auto-Detect Video Parser

/**
* Filter to change the content passed to the Video Parser.
*
* @param string $content Post Content.
* @param object $post Post Object.
*
* @return string Content.
*/
add_filter('rank_math/video/parser_content', function( $content, $post ) {
return $content;
}, 10, 2);