Created
February 18, 2024 17:39
-
-
Save robwent/be32ea0cc824ce90ddc31447ebf67ea4 to your computer and use it in GitHub Desktop.
Example of Yoast update changing custom query vars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Sitemap Test | |
* Description: A simple plugin to test Yoast sitemap integration. | |
*/ | |
function add_to_yoast_sitemap( $custom_sitemaps ) { | |
$custom_sitemaps .= '<sitemap><loc>' . site_url() . '/my-custom-sitemap.xml</loc></sitemap>'; | |
return $custom_sitemaps; | |
} | |
add_filter( 'wpseo_sitemap_index', 'add_to_yoast_sitemap' ); | |
function my_query_vars( $vars ) { | |
$vars[] = "xml_my_custom_sitemap"; | |
return $vars; | |
} | |
add_filter( 'query_vars', 'my_query_vars', 15 ); | |
function rewrite_rules() { | |
add_rewrite_rule( '^my-custom-sitemap\.xml$', 'index.php?xml_my_custom_sitemap=true', 'top' ); | |
} | |
add_action( 'init', 'rewrite_rules', 1 ); | |
function generate_my_sitemap() { | |
global $wp_query; | |
// var_dump($wp_query->query_vars); die; | |
if ( ! empty( $wp_query->query_vars["xml_my_custom_sitemap"] ) ) { | |
$wp_query->is_404 = false; | |
var_dump($wp_query->query_vars); die; | |
// Create your sitemap here | |
} | |
} | |
add_action( 'template_redirect', 'generate_my_sitemap' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment