我想将wp-sitemap.xml重定向到自定义的站点地图文件。我正在使用template_redirect,但是请求没有被重定向。我试图用die()调试它,但是它也不起作用。
function rh_redirect_wordpress_sitemap() {
global $wp;
if ( ! is_404() ) {
return;
}
$uri = $wp->request;
if ( home_url( $uri ) == home_url( 'wp-sitemap.xml' ) ) {
die( 'This is the WordPress sitemap' );
}
}
add_action( 'template_redirect', 'rh_redirect_wordpress_sitemap', 1 );我还禁用了WordPress的站点地图。
add_filter( 'wp_sitemaps_enabled', '__return_false' );发布于 2023-06-01 16:08:51
Yoast使用$_SERVER['REQUEST_URI']全局识别它是否是对核心站点地图的请求,然后重定向。
下面是基于Yoast代码的一些未经测试的代码:
add_action( 'template_redirect', static function () {
// If there is no path, nothing to do.
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
return;
}
$path = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
// If it's not a wp-sitemap request, nothing to do.
if ( substr( $path, 0, 11 ) !== '/wp-sitemap' ) {
return;
}
wp_redirect( 'https://example.com', 301 );
exit;
} );https://wordpress.stackexchange.com/questions/415594
复制相似问题