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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.