Yoast SEO plugin sitemap does a great job in keeping your site’s sitemap up to date. However, if you have an external website like a subdomain you want to link in the sitemap or just a page not linked to WordPress it won’t know all links to add those links.

Thankfully, it allows us to add additional URLs manually. To do this, you need to have access to your active theme’s code. If you don’t have access, connect with your site admin or developer.

To extend the sitemap, we need to edit functions.php file in your active theme, and define a function called add_sitemap_custom_items  like in the code below:

# START EXTENDING YOAST
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/sitemap-1.xml</loc>
 <lastmod>2025-05-25T15:19:00+00:00</lastmod>
 </sitemap>';
 
 return $sitemap_custom_items;
}
# END EXTENDING YOAST

functions.php file helps in tweaking or extending your theme functionality, to learn more about it read its documentation.

That demo code will work but it will have a fixed timestamp. It should ideally be dynamic according to the time it was last modified.

Dynamic timestamp for static file in same server

Assuming your external sitemap is a static file in same server, we can write a function to read when it was last modified. This way it will always update the timestamp in the Yoast SEO plugin. See example code below:

add_filter('wpseo_sitemap_index', 'add_sitemap_custom_items');

function add_sitemap_custom_items($sitemap_custom_items) {
    $filepath = dirname(__FILE__, 5) . '/sitemap-1.xml';

    $lastmod = file_exists($filepath) ? date('c', filemtime($filepath)) : date('c');

    $sitemap_custom_items .= "<sitemap>
        <loc>" . home_url('/sitemap-1.xml') . "</loc>
        <lastmod>{$lastmod}</lastmod>
    </sitemap>";

    return $sitemap_custom_items;
}

The ‘5’ in dirname(__FILE__, 5) is 5 levels from the theme folder, it could be different depending on which folder the static file is.

Dynamic timestamp for non static file

You can create an API to expose the contents of data. Include updated_at or created_at field that will be mapped to the <lastmod> field.

Connect with an expert if not sure.

Credits to this gist, while defining sitemaps for this tools it helped me figure it out.