Skip to content

Instantly share code, notes, and snippets.

@seanlanglands
Created October 18, 2024 20:12
Show Gist options
  • Save seanlanglands/3f3bcba73a42e89f035514eb0ce30459 to your computer and use it in GitHub Desktop.
Save seanlanglands/3f3bcba73a42e89f035514eb0ce30459 to your computer and use it in GitHub Desktop.
<?php
/**
* Enable msm-sitemap plugin sitemap rendering on non-production environments
* by overriding blog_public to 1 on the pre_option hook.
*
* @param mixed $pre_option Value of an existing option.
*
* @return mixed
*/
function vip_enable_non_prod_sitemap_rendering( $pre_option ) {
// Return early and leave blog_public option as is on production.
if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) && 'production' === VIP_GO_APP_ENVIRONMENT ) {
return $pre_option;
}
// Support both /sitemap.xml and /sitemap.xml?yyyy=2024&mm=10&dd=14 type variations.
$request_uri = strval( filter_var( $_SERVER['REQUEST_URI'] ?? '', FILTER_SANITIZE_URL ) );
$parsed_url = parse_url( $request_uri );
// Check if the path is `/sitemap.xml` and make blog_public option true.
if ( isset( $parsed_url['path'] ) && $parsed_url['path'] === '/sitemap.xml' ) {
return 1;
}
// Fallback to return default value.
return $pre_option;
}
add_filter( 'pre_option_blog_public', 'vip_enable_non_prod_sitemap_rendering' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment