Skip to content

Instantly share code, notes, and snippets.

@seanlanglands
Last active August 29, 2023 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanlanglands/89d4e00e5ce49831f5525c502aca525d to your computer and use it in GitHub Desktop.
Save seanlanglands/89d4e00e5ce49831f5525c502aca525d to your computer and use it in GitHub Desktop.
Custom wp-sitemap routes and serve via siteurl value
<?php
// Disable sitemap stylesheets if needed.
add_filter( 'wp_sitemaps_stylesheet_url', '__return_false' );
add_filter( 'wp_sitemaps_stylesheet_index_url', '__return_false');
add_action( 'init', 'register_custom_sitemap_rewrites', 20 );
/**
* Register custom `^sitemap/*` rewrite rules for sitemap routes.
*
* @return void
*/
function register_custom_sitemap_rewrites(): void {
// Register index route.
add_rewrite_rule( '^sitemap/wp-sitemap\.xml$', 'index.php?sitemap=index', 'top' );
// Register custom rewrites for the XSL stylesheet if needed.
//add_rewrite_rule( '^sitemap/wp-sitemap\.xsl$', 'index.php?sitemap-stylesheet=sitemap', 'top' );
//add_rewrite_rule( '^sitemap/wp-sitemap-index\.xsl$', 'index.php?sitemap-stylesheet=index', 'top' );
// Register routes for providers.
add_rewrite_rule(
'^sitemap/wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&sitemap-subtype=$matches[2]&paged=$matches[3]',
'top'
);
add_rewrite_rule(
'^sitemap/wp-sitemap-([a-z]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&paged=$matches[2]',
'top'
);
}
/**
* Setting `home` and `siteurl` options to different values helps us set
* permalinks correctly, but it causes some problems for resources that we still
* want to serve from WordPress. This ultility function filters those resources to use the home url.
*
* @param string $resource_url URL of a WordPress resource.
*
* @return string
*/
function update_resource_url( string $resource_url ): string {
$home_path = wp_make_link_relative( home_url() );
$resource_path = wp_make_link_relative( $resource_url );
if ( ! empty( $home_path ) ) {
$resource_path = preg_replace( sprintf( '#^%s/*#', $home_path ), '/', $resource_path );
}
return home_url( $resource_path );
}
add_filter( 'home_url', 'fix_wp_sitemap_url', 10, 2 );
/**
* Filters the home URL for any `/wp-sitemap*` requests, as we want these to use
* the `siteurl` option value instead of `home`. It also prepends `/sitemap` to match
* our custom rewrite rules.
*
* Example request paths:
* - /sitemap/wp-sitemap.xml
* - /sitemap/wp-sitemap-posts-post-1.xml
* - /sitemap/wp-sitemap-news-10.xml
* - /sitemap/wp-sitemap-posts-topic-1.xml
*
* @param string $url The complete home URL including scheme and path.
* @param string $path Path relative to the home URL. Blank string if no path is specified.
*
* @return string
*/
function fix_wp_sitemap_url( string $url, string $path ): string {
if ( str_starts_with( $path, '/wp-sitemap' ) ) {
return site_url( '/sitemap' . $path );
}
return $url;
}
add_filter( 'wp_sitemaps_index_entry', 'fix_wp_sitemaps_index_entry_urls', 10, 1 );
/**
* Filters the sitemap entry for the sitemap index.
*
* @uses update_resource_url() to update the URL to use siteurl instead of home URL.
*
* @param array $sitemap_entry Sitemap entry for the post.
*/
function fix_wp_sitemaps_index_entry_urls( array $sitemap_entry ): array {
$sitemap_entry['loc'] = update_resource_url( $sitemap_entry['loc'] );
return $sitemap_entry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment