Rewrite the url and redirect to this and get the query string values
-
Hi Team,
I am creating a url by add_query_arg() by adding some query string to the url in a ajax call. So the new url will be like this https://example.com/seminar/?id=12 , where seminar is a page having page slug seminar.
I’m sending this url in ajax respone and redirecting to it.
So, I want that the url should automatically turn into like this:
https://example.com/seminar/12and I also want to use id parameter.
Currently I’m coding like this:
add_action(‘init’, ‘custom_rewrite_rule’);
add_filter(‘query_vars’, ‘wpse12965_query_vars’ );
add_action( ‘init’,’my_flush_rules’,50 );// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( ‘rewrite_rules’ );if ( ! isset( $rules[‘seminar/([^/]+)/?$’] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}}
function wpse12965_query_vars( $query_vars )
{
$query_vars[] = ‘id’;return $query_vars;
}
function custom_rewrite_rule() {add_rewrite_rule(‘seminar/([^/]+)/?$’,’index.php?pagename=$matches[1]&id=$matches[2]’,’top’);
}
But it seems like not working. Niether the url is changing nor the id parameter is adding to query_vars[] in $wp_query variable.
Anyone, Please suggest.
Thanks,
Darik
- The topic ‘Rewrite the url and redirect to this and get the query string values’ is closed to new replies.