How to properly rewrite url by custom var

I’ve been trying to rewrite a shop uri, and what I have now is this code:

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('wp_loaded','flushRules');  

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules) {
    $newrules = array();
    $newrules['shop/brand/(brand)/?$'] = 'shop.php?brand=$matches[1]' ; 
     //$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
     return $newrules + $rules ;
}

// Adding the bid var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars) {
    array_push($vars, 'brand');
    return $vars;
}

but no matter what I do I can’t get this to work.
I’m using Monkeyman Rewrite Analyzer plugin to view the active ap rewrites and what it tells me is that brand “is not public and will not be saved”. Also, it changes my shop.php to shop_php…

Read More
shop/brand/(brand)/?$    shop_php?brand: (brand)

Why is this such a mess? I tried to modify .htaccess as little as possible; also, since I want this to be in the theme.

Related posts

Leave a Reply

3 comments

  1. So, first off, never flush rewrite rules on every page load. Better to do it on plugin activation one time.

    Step 1: add the rewrite rule:

    add_action( 'init', 'wpse26555_add_rewrite' );
    function wpse26555_add_rewrite()
    {
        // You should probably rewrite to index.php instead of shop.php?
        add_rewrite_rule( '/shop/brand/([^/]+)/?$', 'index.php?brand=$matches[1]', 'top' );
    }
    

    Step 2: add the query var

    add_filter( 'query_vars', 'wpse26555_add_vars' );
    function wpse26555_add_vars( $vars )
    {
        $vars[] = 'brand';
        return $vars;
    }
    

    Then flush on plugin activation (because something like this should probably be in a plugin file).

    <?php    
    // Somewhere in your plugin's main file    
    register_activation_hook( __FILE__, 'wpse26555_activation' );
    function wpse26555_activation()
    {
            // Add the rule first
            wpse26555_add_rewrite();
            // Then flush rewrite rules
            flush_rewrite_rules();
    }
    

    Then on the front end you can do something like this:

    if( $brand = get_query_var( 'brand' ) )
    {
        // Do stuff with $brand here
    
    }
    

    Maybe a better strategy would be to rewrite brand to a specific page? Or maybe brand could be some sort of custom taxonomy?

    You’ll have to test the above code, it was off the top of my head, but it should work or at least get you started

  2. thanks for the lengthy reply. The thing is, im not making a plugin here, im just trying to interact with one.
    its a shop plugin [theCartPress], and i made my own design all around it, and made a shop.php page for a custom post type of that kind [tcp_product].

    i do get all posts of that post type rerouted to the right file, but i also want to be able to display them by brand. i have some _GET in it so i can do ?brand=mybrand, but i want to use the wordpress permalinks. i actually do have a custom taxonomy for it. That’s what i use after my GET:

     if (($_GET['brand'] == 'owl') || !isset($_GET['brand'])) {
        $brand = 'owl';
      } else {
        $brand = $_GET['brand'];
      }
      $args = array( 'post_type' => 'tcp_product', 'posts_per_page' => 30, 'tax_query' => array(
        array('taxonomy' => 'tcp_product_supplier', 'field' => 'slug', 'terms' => $brand)));
    
    $loop = new WP_Query( $args );
    

    sorry to mods for using the answer post, but the comment reformatting is impossible..

  3. the top answer works, but there is a more proper and official way of doing these types of rewrites…

    for example the following code will register your custom query variable and register your rewrite rule from the same action:

    function include_cpt_rewrites(){
      add_rewrite_tag('%brand%', '([a-zA-Z0-9-]+)');
    
      add_rewrite_rule('^shop/brand/([^/]+)/?$', 'index.php?brand=$matches[1]', 'top');
    }
    add_action('init', 'include_cpt_rewrites');
    

    the final step of flushing the permalinks can be manually done from the WP admin instead of doing it by code by going to settings/permalinks and pressing on save changes.

    add_rewrite_tags / add_rewrite_rule