Custom WordPress Sitemaps with Yoast

Written by
Published
Updated
Typical Read
3 minutes

Learn how to create a custom WordPress sitemap using Yoast. Easily extend your sitemap index with dynamic URLs & custom post types.

Give your site’s SEO a boost by creating custom WordPress sitemaps for your dynamically generated URLs, custom post types, and more. It’s easy using Yoast by extending your site’s sitemap index with your own registered sitemap and defined URLs.

tl;dr

/**
 * Add the custom sitemap to the sitemap index.
 * 
 * @since x.x.x
 * @link https://developer.yoast.com/features/xml-sitemaps/api/#adding-content
 * 
 * @return string XML sitemap index.
 */
function yourplugin_sitemap_index( $sitemap_index ) {
  global $wpseo_sitemaps;

  $sitemap_url    = home_url( 'yourplugin-sitemap.xml' );
  $sitemap_date   = date(DATE_W3C);
  $custom_sitemap = <<<SITEMAP_INDEX_ENTRY
<sitemap>
  <loc>%s</loc>
  <lastmod>%s</lastmod>
</sitemap>
SITEMAP_INDEX_ENTRY;
  $sitemap_index .= sprintf( $custom_sitemap, $sitemap_url, $sitemap_date );

  return $sitemap_index;
}
add_filter( 'wpseo_sitemap_index', 'yourplugin_sitemap_index' );

/**
 * Register the custom sitemap.
 * 
 * @since x.x.x
 * @link https://developer.yoast.com/features/xml-sitemaps/api/#add-a-custom-post-type
 * 
 * @return void
 */
function yourplugin_sitemap_register() {
  global $wpseo_sitemaps;

  if ( isset( $wpseo_sitemaps ) && ! empty( $wpseo_sitemaps ) ) {
    $wpseo_sitemaps->register_sitemap( 'yourplugin', 'yourplugin_sitemap_generate' );
  }
}
add_action( 'init', 'yourplugin_sitemap_register' );

/**
 * Add URLs to the custom sitemap.
 * 
 * @since x.x.x
 * 
 * @return void
 */
function yourplugin_sitemap_generate() {
  global $wpseo_sitemaps;

  $urls = [];
  $urls[] = $wpseo_sitemaps->renderer->sitemap_url([
    'mod'    => date( 'c', strtotime( 'date_modified' ) ),
    'loc'    => 'https://page.url',
    'images' => [
      [
        'src'   => 'https://image.url',
        'title' => 'Image title',
        'alt'   => 'Image alt',
      ]
    ]
  ]);

  $sitemap_body = <<<SITEMAP_BODY
<urlset
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd"
  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
%s
</urlset>
SITEMAP_BODY;
  $sitemap = sprintf( $sitemap_body, implode( "\n", $urls ) );
  $wpseo_sitemaps->set_sitemap( $sitemap );
}

Custom WordPress sitemaps

WordPress sitemap helps boost your site’s visibility, rankings, and should be prioritized no matter what type of site you run. It helps search engines find internal and hard to find pages allowing them to crawl and index your site more easily. Thus, it’ll improve your web pages’ visibility to search engines.

What is a sitemap?

A sitemap is an XML list of all available pages on a site. They are even more important today from an SEO (Search Engine Optimization) point of view. Here’s a few reasons why:

  • Sitemaps inform search engines about the changes to your site structure
  • They help bots crawl and index your site faster
  • It helps search engines to index large sites that aren’t structured well or interlinked
  • Sitemaps let you tell search engines to prioritize pages that are more important

It’s simple to see why you’d want to prioritize building & optimizing sitemaps. After all, it’s a great way to bring organic traffic to your site. If you’re not using XML sitemaps, you’re missing out on an effective SEO tool.

WordPress makes creating sitemaps easy.

The good news is, sitemaps in WordPress are a cinch. By default, they’re included on every WordPress site, fairly basic, could and should be improved on if you want to take full advantage of them. You can view it by going to sitemap.xml (e.g. https://yoursite.com/sitemap.xml).

Not ready for search engines to crawl? You can easily let crawlers know you’re not ready for them by setting your site to private. Do this by going to Settings > General > Privacy.

WordPress sitemaps are available to every search engine that supports the protocol, including Google, Yahoo!, Bing, Ask.com, and others. In addition, WordPress.com automatically sends updates to search engines every time you update or delete a page or post.

Enhance your WordPress sitemaps with Yoast

Yoast SEO is one of the most popular plugins for improving WordPress website’s SEO. This plugin takes care of all the technical aspects related to content and helps you track the keyword density and readability. It can also assist you in creating XML sitemaps.

Join the conversation.

Your email address will not be published. Required fields are marked *

All comments posted on 'Custom WordPress Sitemaps with Yoast' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.