• Resolved Darik

    (@darikk)


    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/12

    and 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

Viewing 1 replies (of 1 total)
  • Thread Starter Darik

    (@darikk)

    Hi All,

    I have solved this issue myself. Please have a look to my modified code.

    Note : First important thing is that you must write you rewrite rule , filter query var and flush rewrite rule only in plugin activation hook.

    register_activation_hook(__FILE__, “call_rewrite_fun”));

    function call_rewrite_fun() {

    // Filter query var
    init_query_vars();

    // Add rewrite rule
    add_endpoints();

    // Flush
    flush_rewrite_rules();
    }

    you should use template_redirect hook for using get_query_var() function.

    Thanks,
    Darik

Viewing 1 replies (of 1 total)
  • The topic ‘Rewrite the url and redirect to this and get the query string values’ is closed to new replies.