Improve WordPress SEO with XML sitemaps and robots.txt without a plugin

XML sitemap and robots.txt in WordPress without an SEO plugin

Since WordPress 5.5 every site has an XML sitemap at /wp-sitemap.xml, and the virtual robots.txt already points to it. Older tutorials build a sitemap with a rewrite rule and a loop of echo statements; today that duplicates core and usually stops at a few hundred URLs. What remains is to configure the core sitemap and the robots file.

Problem: A site without an SEO plugin needs a sitemap that lists only the content worth indexing and a robots.txt that keeps crawlers out of useless URLs.

Solution: Keep the core sitemap and adjust it with its filters (wp_sitemaps_post_types, wp_sitemaps_taxonomies, wp_sitemaps_add_provider, wp_sitemaps_posts_query_args), and add rules to the virtual robots file with the robots_txt filter.

What core already does

/wp-sitemap.xml is an index with sub-sitemaps for each public post type, each public taxonomy and the authors, up to 2,000 URLs per file. It is disabled when “Discourage search engines” is checked in Settings, Reading. The default robots.txt blocks /wp-admin/, allows admin-ajax.php and adds the Sitemap: line.

Remove what should not be indexed

<?php
// No user sitemap (author archives are thin on a one-author blog)
add_filter( 'wp_sitemaps_add_provider', function ( $provider, $name ) {
    return 'users' === $name ? false : $provider;
}, 10, 2 );

// No sitemap for a post type or a taxonomy
add_filter( 'wp_sitemaps_post_types', function ( $post_types ) {
    unset( $post_types['attachment'], $post_types['ha_testimonial'] );
    return $post_types;
} );

add_filter( 'wp_sitemaps_taxonomies', function ( $taxonomies ) {
    unset( $taxonomies['post_tag'] );
    return $taxonomies;
} );

// Leave single posts out, e.g. the ones marked with a custom field
add_filter( 'wp_sitemaps_posts_query_args', function ( $args, $post_type ) {
    if ( 'post' === $post_type ) {
        $args['meta_query'] = [
            [ 'key' => '_ha_hide_in_sitemap', 'compare' => 'NOT EXISTS' ],
        ];
    }
    return $args;
}, 10, 2 );

A URL that is excluded from the sitemap can still be indexed; the sitemap is a hint. For pages that must not appear in search results, a noindex robots meta tag is needed as well.

robots.txt

<?php
add_filter( 'robots_txt', function ( $output, $public ) {
    if ( ! $public ) {
        return $output; // "Discourage search engines" is on, keep the core Disallow: /
    }
    $output .= "Disallow: /?s=\n";
    $output .= "Disallow: /search/\n";
    return $output;
}, 10, 2 );

The filter works only while there is no physical robots.txt file in the site root; a real file is served by the web server and WordPress never runs. Do not block /wp-content/ or /wp-includes/: Google needs the CSS and JavaScript to render the pages.

Check

curl -s https://example.com/robots.txt
curl -s https://example.com/wp-sitemap.xml | grep -o '<loc>[^<]*</loc>'

NOTE: After changing the filters, submit https://example.com/wp-sitemap.xml in Google Search Console once. If an SEO plugin is installed later, it disables the core sitemap and serves its own; the filters above then have no effect.