WordPress Snippets

Code Snippet

After doing a migration from Yoast SEO to The SEO Framework, you may find that you cannot get rid of the /sitemap_index.xml file which is no longer needed since it was the Yoast version.

The SEO Framework creates a sitemap at /sitemap.xml on the fly, and this is the one we’d like to use. And even though Yoast also creates their version of the sitemap at /sitemap_index.xml on the fly, and you’ve deleted the Yoast plugin, the file still “lives” in that location somehow.

I’m told it’s actually a hosting issue (don’t know).

No worries though, because it’s now been taken over by The SEO Framework, which also keeps its sitemap at /sitemap.xml. This means that you now have exact duplicate sitemaps living in two locations.

Not a big deal.

However, you may be uncomfortable with this, as it seems wrong. But since the file doesn’t actually exist, you can’t simply go in with FTP and delete it.

But with the following code, you can kill it off making The SEO Framework /sitemap.xml the only sitemap.

PHP

Place the following code anywhere in your child theme’s functions.php document.

add_filter( 'the_seo_framework_sitemap_endpoint_list', function( $list ) {
    if ( isset( $list['base'] ) ) {
        // set human-readable name
        $list['base']['endpoint'] = 'sitemap.xml';
        // set custom computer name
        $list['base']['regex']    = '/^sitemap\.xml/';
        // remove from robots.txt listing
        $list['base']['robots']   = false;
    }
    // remove yoast /sitemap_index.xml
    unset( $list['index'] );

    return $list;
} );

Note

All modifications to a theme or plugin should be made by creating a child theme and placing the changes there. Changes made to the parent theme will be overwritten the next time it updates.


WordPress Notes:

  • All modifications to a theme or plugin should be made by creating a child theme and placing the changes there; changes made to the parent theme will be overwritten the next time it updates

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.