Currently Available: Need a skilled Software Developer for your next project?
Categories
WordPress

How to Build Custom Sitemap Plugins for WordPress

WordPress includes an XML sitemap system that lists public URLs for search engines to crawl. When a plugin needs to add URLs from a custom data source, you can extend that system with a sitemap provider, a PHP class that supplies URLs page by page and tells WordPress how many sitemap pages to expose. WordPress introduced this provider API in version 5.5.

Users and contributors also need clear documentation to work with your plugin. Your code defines which URLs belong in the sitemap, and your documentation explains those rules, how to install and test the plugin, and how to contribute. Note that the Contributors: field in a WordPress.org readme only gives public credit. It does not document the plugin or grant repository access.

Decide what the sitemap should include

Before you write any code, check whether WordPress’s built-in sitemap already lists the content you care about. You only need a custom provider when your plugin pulls URLs from a data source WordPress core does not cover, or when you need different rules for deciding which URLs appear.

Decide on your data source and inclusion rules up front. For example, your plugin might list published entries from a custom post type named resource. Every entry should point to a public URL that the site owner wants search engines to crawl. Leave out drafts, private records, and URLs that redirect somewhere else.

Always use absolute URLs, such as https://example.com/resources/item/, instead of relative paths. Google recommends listing only the URLs a site owner wants in search results, though a sitemap never guarantees that a search engine will crawl or index every URL you list. Check Google’s sitemap guidance for URL and file-size limits.

Add a provider to WordPress

To build a provider, you implement two methods on a class that extends WP_Sitemaps_Provider. The get_url_list() method returns the entries for a single sitemap page, and get_max_num_pages() tells WordPress how many pages it needs to hold all of your entries. WordPress calls those methods to build the sitemap page URLs and generate the XML. You can read the provider class reference for the full requirements.

The plugin example below adds published resource posts to the sitemap. It registers the provider when WordPress initializes its sitemap system, lets WordPress core handle the XML output, and includes each entry’s last modification date.

name        = 'resources';
                    $this->object_type = 'resource';
                }

                private function page_size() {
                    $limit = function_exists( 'wp_sitemaps_get_max_urls' )
                        ? wp_sitemaps_get_max_urls( $this->object_type )
                        : 2000;

                    return max( 1, min( 50000, absint( $limit ) ) );
                }

                public function get_url_list( $page_num, $object_subtype = '' ) {
                    $query = new WP_Query(
                        array(
                            'post_type'              => 'resource',
                            'post_status'            => 'publish',
                            'posts_per_page'         => $this->page_size(),
                            'paged'                  => max( 1, absint( $page_num ) ),
                            'orderby'                => 'ID',
                            'order'                  => 'ASC',
                            'no_found_rows'          => true,
                            'ignore_sticky_posts'    => true,
                            'update_post_meta_cache' => false,
                            'update_post_term_cache' => false,
                        )
                    );

                    $urls = array();

                    foreach ( $query->posts as $post ) {
                        $entry = array(
                            'loc' => get_permalink( $post ),
                        );

                        $lastmod = get_post_modified_time( DATE_W3C, true, $post );

                        if ( $lastmod ) {
                            $entry['lastmod'] = $lastmod;
                        }

                        $urls[] = $entry;
                    }

                    return $urls;
                }

                public function get_max_num_pages( $object_subtype = '' ) {
                    $query = new WP_Query(
                        array(
                            'post_type'              => 'resource',
                            'post_status'            => 'publish',
                            'posts_per_page'         => $this->page_size(),
                            'paged'                  => 1,
                            'fields'                 => 'ids',
                            'no_found_rows'          => false,
                            'update_post_meta_cache' => false,
                            'update_post_term_cache' => false,
                        )
                    );

                    return (int) $query->max_num_pages;
                }
            }
        }

        wp_register_sitemap_provider(
            'resources',
            new Acme_Resource_Sitemap_Provider()
        );
    }
);

The provider name resources sets the URL slug WordPress uses for this provider’s sitemap pages. Calling wp_register_sitemap_provider() registers the class with WordPress. Once you activate the plugin, open /wp-sitemap.xml and verify that the index lists your new provider.

Make sure your page count matches the entries your provider returns. If get_max_num_pages() returns a number that is too high, WordPress will add links to empty pages in the sitemap index. If it returns a number that is too low, WordPress will leave URLs out of the sitemap. The example above queries the database directly to count pages. If your plugin pulls from a very large data source, measure how long that count query takes and optimize it before it slows down the site.

WordPress core defaults to 2,000 URLs per sitemap and lets you filter that limit. Google sets a separate maximum of 50,000 URLs or 50 MB uncompressed for any single sitemap file. Because WordPress and Google enforce these limits at different levels, avoid raising the WordPress setting to match Google’s ceiling. Review the core URL-limit implementation and measure the size of your generated XML file, especially if your site uses long URLs.

Test the routes and URL rules

Before you ship the plugin, test the sitemap index and at least one provider page on a development or staging site. Confirm that the index lists your provider, that each page returns valid XML, and that every URL points to the intended public content. Check the pagination as well to make sure no URLs are missing or duplicated across pages. Finally, test an empty result set and a page number higher than get_max_num_pages() returns.

Test your plugin alongside the exact sitemap setup running on the target site. WordPress’s core provider API handles routing and XML rendering for registered providers, but it does not control how third-party SEO plugins or custom sitemap replacements behave. Read the documentation for whichever sitemap plugin the site uses and check which sitemap URL it serves. If you run into conflicts between plugins, see WordPress plugin and theme conflicts for troubleshooting steps.

Document the plugin for users and contributors

Your WordPress.org readme.txt file should tell users what the plugin adds to the sitemap, how to install and configure it, which WordPress version it requires, and how to report problems. WordPress.org parses this file to build your plugin’s directory page, pulling the version tied to the release’s stable tag. Its Contributors: field lists WordPress.org usernames for public credit. You can check the readme documentation for the exact formatting rules.

Keep deeper technical details in a repository file or a dedicated documentation site, and link to it from the readme. Explain which data source the provider queries, which records it excludes, how it paginates entries, and how a contributor can reproduce a bug and run tests. Include the sitemap index URL and a sample provider page URL from a test installation so readers can compare your documentation against live output.

Treat public credit and repository access as two separate things. Adding someone’s WordPress.org username to readme.txt does not let them commit code or manage the plugin directory listing. Review the contributor roles documentation to see how WordPress.org handles permissions, and grant repository or directory access separately.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

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