Halloween Sale 15% off Ends in See the plans
PHP

Add Custom URLs to the XML Sitemap

Adds a sitemap of extra URLs, such as static landing pages or sections served outside WordPress, to the built-in sitemap index.

Don’t have WPIDE yet? Install it on your site

Code

// Extra URLs to include.
$wpide_extra_sitemap_urls = array(
    home_url( '/landing/spring-offer/' ),
    home_url( '/docs/' ),
);

add_action( 'init', function () use ( $wpide_extra_sitemap_urls ) {
    if ( ! class_exists( 'WP_Sitemaps_Provider' ) || ! wp_sitemaps_get_server()->sitemaps_enabled() ) {
        return;
    }

    if ( ! class_exists( 'WPIDE_Extra_Urls_Sitemap' ) ) {
        class WPIDE_Extra_Urls_Sitemap extends WP_Sitemaps_Provider {
            protected $urls = array();

            public function __construct( array $urls ) {
                $this->name        = 'extra';
                $this->object_type = 'extra';
                $this->urls        = $urls;
            }

            public function get_url_list( $page_num, $object_subtype = '' ) {
                return array_map( function ( $url ) {
                    return array( 'loc' => esc_url_raw( $url ) );
                }, $this->urls );
            }

            public function get_max_num_pages( $object_subtype = '' ) {
                return $this->urls ? 1 : 0;
            }
        }
    }

    wp_register_sitemap_provider( 'extra', new WPIDE_Extra_Urls_Sitemap( $wpide_extra_sitemap_urls ) );
}, 20 );

Notes

List full URLs on your own domain. The new sitemap appears at /wp-sitemap-extra-1.xml and in /wp-sitemap.xml.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.