Answer the question
In order to leave comments, you need to log in
How to create a rewrite rule in Wordpress?
Hello! It is required to create a rewrite rule in the plugin. Trying to understand how it works, I wrote the following code:
add_filter( 'query_vars', 'add_query_vars' );
function add_query_vars( $vars ) {
$vars[] = 'my_param';
return $vars;
}
add_action( 'generate_rewrite_rules', 'add_my_rules' );
function add_my_rules() {
add_rewrite_rule( 'my_param/(\d+)?$', 'index.php?my_param=$matches[1]', 'top' );
}
add_action('wp', "test_cm");
function test_cm(){
$my_param_value = get_query_var('my_param');
echo $my_param_value;
}
Answer the question
In order to leave comments, you need to log in
There is an example in CODEX, in my opinion, illustrative:
function custom_rewrite_basic() {
add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question