Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 68 ยท Using the Dashboard

Sitemaps, SEO Basics and a Table of Contents

Learn how the WordPress XML sitemap works, add an HTML sitemap, set up SEO basics such as titles and canonical URLs, and generate a table of contents from headings.

A WordPress site can be perfect for visitors and still be invisible to search engines. Search engine optimization (SEO) is the craft of making sure crawlers can find your pages, understand them and present them well in results. Much of it is simple housekeeping that WordPress already supports: clean permalinks, proper headings, sitemaps, descriptive titles and a clear internal link structure.

This lesson focuses on three concrete tools. The XML sitemap tells search engines which URLs exist. The HTML sitemap shows the same structure to human visitors. A table of contents helps readers and search engines navigate a long article. You will build small versions of all three in PHP (stand-ins, since WordPress cannot run on this site; WordPress code in code blocks is read-only) so that you understand what plugins and core are doing for you.

What search engines need from a site

Need WordPress feature
Discover every important URL XML sitemap, internal links
Understand what a page is about Title, headings, meta description, alt text
One address per page Permalinks, canonical URL
Know what not to index noindex robots meta, robots.txt, search visibility setting
Trust and speed HTTPS, fast pages, mobile-friendly theme (see Performance Optimization)

The XML sitemap

An XML sitemap is a machine-readable list of the URLs you want indexed, with optional last-modified dates. Search engines use it to discover pages faster and to learn when something changed. It does not guarantee indexing or ranking, but for a large or new site it is very helpful.

Since WordPress 5.5 core generates a sitemap automatically:

  • The index is at /wp-sitemap.xml and lists the sub-sitemaps.
  • The sub-sitemaps are split by object type, for example /wp-sitemap-posts-post-1.xml, /wp-sitemap-posts-page-1.xml, /wp-sitemap-taxonomies-category-1.xml and /wp-sitemap-users-1.xml.
  • Each file holds at most 2,000 URLs, and WordPress adds more files as the site grows.
  • Posts with the status private, drafts and posts set to noindex are left out.
  • If Settings > Reading > Discourage search engines is on, the sitemap is disabled.

SEO plugins such as Yoast SEO, Rank Math and All in One SEO replace the core sitemap with their own (/sitemap_index.xml) and give you more control: exclude pages, add images and news. Use one sitemap source only; do not run two at once.

To change or disable the core sitemap in code (read-only example):

PHP
// disable completely
add_filter( 'wp_sitemaps_enabled', '__return_false' );

// remove the users sitemap (often exposes author names)
add_filter( 'wp_sitemaps_add_provider', function ( $provider, $name ) {
    return 'users' === $name ? false : $provider;
}, 10, 2 );

// exclude a post type from the sitemap
add_filter( 'wp_sitemaps_post_types', function ( array $types ): array {
    unset( $types['attachment'] );
    return $types;
} );

Once your sitemap is live, tell search engines: add the line Sitemap: https://example.com/wp-sitemap.xml to robots.txt (core and SEO plugins add it to the virtual robots.txt automatically), and submit the URL in Google Search Console and Bing Webmaster Tools to see which pages were indexed and why others were not.

What a sitemap looks like

A sitemap is plain XML in the sitemaps.org format. The generator below builds one from a list of posts, escapes special characters (an unescaped & makes the file invalid), formats lastmod as an ISO 8601 date and splits long lists into several files with an index, the way core does.

PHP Generate an XML sitemap and index (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Notice & in the second URL, the ISO dates with a time-zone offset, and the index pointing at three files.

The HTML sitemap

An HTML sitemap is an ordinary page that lists your pages (and often categories and recent posts) as links. It is for people, such as visitors who cannot find something, and it also gives crawlers a plain-HTML route to deep pages. It improves usability and internal linking, and is useful on big sites; small sites with a good menu rarely need one.

Ways to add it:

  • A plugin such as Simple Sitemap: install, activate, create a page called Sitemap, add the shortcode (or block) the plugin provides, and optionally group content by type.
  • The core block Page List, or a shortcode that wraps wp_list_pages() (read-only):
PHP
add_shortcode( 'html_sitemap', function (): string {
    $pages = wp_list_pages( [ 'title_li' => '', 'echo' => false ] );   // nested <li> for the page tree
    return '<ul class="sitemap">' . $pages . '</ul>';
} );

Put the link to it in the footer. Use descriptive titles, group the entries sensibly (pages, posts by category) and keep the list current. The stand-in below does what wp_list_pages() does: it turns a flat list of pages with parent IDs into a nested, escaped list.

PHP Nested HTML sitemap from pages (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

SEO basics inside WordPress

A short checklist you can apply to every page:

  1. Permalinks. Use /%postname%/ and short, readable slugs without dates or stop words (see Media Library, Site Settings, Permalinks and Comments). Never change a published URL without a 301 redirect.
  2. Title and meta description. The <title> is the clickable headline in results; aim for about 50 to 60 characters with the main idea first. The meta description (120 to 160 characters) is the snippet; it does not affect ranking directly but affects clicks. Themes with add_theme_support( 'title-tag' ) print the title; an SEO plugin lets you edit both per page. See Core Hooks You Will Use for wp_head.
  3. One <h1> and a clear heading outline. The post title is the h1; use h2 and h3 for sections in order, without skipping levels.
  4. Alt text, descriptive file names and compressed images.
  5. Internal links. Link related posts with descriptive anchor text; this spreads authority and helps discovery.
  6. Canonical URL. <link rel="canonical" href="..."> names the preferred address when the same content is reachable at several URLs (with tracking parameters, a copied post or paginated archives). WordPress prints it automatically for singular content, and SEO plugins add controls.
  7. noindex where appropriate. Thank-you pages, internal search results and thin archives should not compete with real content. Use the SEO plugin's per-page option or wp_robots (see below).
  8. Speed, mobile, HTTPS. Ranking signals, and good for visitors anyway.
  9. Structured data (schema.org JSON-LD) helps results show ratings, FAQs and breadcrumbs; SEO plugins add basic types.

robots.txt controls crawling, noindex controls indexing. A page blocked in robots.txt cannot be crawled, so a noindex tag on it is never seen. To keep a page out of results, allow crawling and use noindex. Read-only example for core's robots API:

PHP
add_filter( 'wp_robots', function ( array $robots ): array {
    if ( is_search() || is_page( 'thank-you' ) ) {
        $robots['noindex'] = true;
        $robots['follow']  = true;
    }
    return $robots;
} );

A table of contents

For long articles a table of contents (TOC) at the top lets readers jump straight to the section they want, makes the structure visible at a glance and earns "jump to" links under your result in Google. It needs two things: an anchor id on every heading and a list of links to those anchors.

In the editor, give a heading an anchor in Advanced > HTML anchor, or add the core Table of Contents block (a list built from the headings automatically). For many posts, automate it: hook the the_content filter, find the headings, add id attributes and prepend a list. The stand-in below shows the algorithm in plain PHP, using the same heading levels (h2 and h3) most sites use, with a slug and a duplicate guard.

PHP Build a table of contents from headings (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Both Why it matters headings get different ids (why-it-matters and why-it-matters-2), so the links point to the right section, and the level-three headings are nested under their parent item. Use a TOC for articles with at least three or four headings; on short posts it only adds clutter. Make sure the links are real anchors (not JavaScript only) and that the anchors have a small scroll-margin-top in CSS so a sticky header does not cover the heading after the jump.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where does WordPress core publish its XML sitemap index?

    Since WordPress 5.5 the index lives at /wp-sitemap.xml, and it links to sub-sitemaps by post type, taxonomy and users.
  2. 2. Why must special characters such as & be escaped in a sitemap?

    A sitemap is XML. Use & so the file parses.
  3. 3. What is the difference between an XML sitemap and an HTML sitemap?

    XML is machine-readable and submitted to search engines; an HTML sitemap is a normal page of links.
  4. 4. A page is blocked in robots.txt and also has a noindex tag. What happens?

    Blocking crawling prevents the crawler from reading the noindex directive. To de-index a page, allow crawling and use noindex.
  5. 5. What two things does a table of contents need?

    Each heading needs a unique id, and the list links to those ids using #fragment URLs.

Summary

  • An XML sitemap lists the URLs you want indexed; core generates /wp-sitemap.xml with up to 2,000 URLs per file, and SEO plugins can replace it.
  • Submit the sitemap in Search Console and Bing Webmaster Tools and reference it in robots.txt.
  • An HTML sitemap is a page of links for people and a plain route for crawlers.
  • Basic SEO: clean permalinks, good titles and descriptions, one h1 with an ordered outline, alt text, internal links, canonical URLs and noindex for thin pages.
  • robots.txt controls crawling and noindex controls indexing; do not combine them to de-index a page.
  • A table of contents needs unique heading anchors and a list of links; the block editor, a plugin or the the_content filter can produce it.

Next: The Loop if you want to see how templates print this content, or return to the WordPress overview.