I added the following custom routes to WordPress:
function add_custom_rewrite_rules() {
add_rewrite_rule( 'generate/?', 'index.php?custom_route=generate', 'top' );
add_rewrite_rule( 'random/?', 'index.php?custom_route=random', 'top' );
}
add_action( 'init', 'add_custom_rewrite_rules', PHP_INT_MAX );
This is the callback function:
function custom_routes_handler() {
$custom_route = get_query_var( 'custom_route' );
var_dump( $custom_route);
global $wp_rewrite;
foreach ( $wp_rewrite->rewrite_rules() as $rule => $rewrite ) {
echo $rule . ' => ' . $rewrite . '<br>';
}
exit;
}
I get the var_dump result of string(0) ""
. And in the listed routes I can see, among all other routes:
generate/?$ => index.php?custom_route=generate
random/?$ => index.php?custom_route=random
I also triggered the URL rewrites regeneration via Admin > Settings > Save permalinks.
The WordPress installation is in the root folder.
Why am I not getting the proper value for get_query_var( 'custom_route' )
?