WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

ive been trying to rewrite a shop uri, and what i have now is this code:


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

// 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 cant get this to work. im 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...

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

why is this such a mess? i try to modify .htaccess as little as possible, also since i want this to be in the theme.

aaaaa thanks

share|improve this question

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).

// Somewhere in your plugin's main file    
register_activation_hook( __FILE__, 'wpse26555_activation' );
function wpse26555_activation()
        // Add the rule first
        // Then 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

share|improve this answer
Hey Christopher, i posted another 'answer' to my question - the comments reformat was breaking the code.. – a deer Aug 24 '11 at 17:36

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..

share|improve this answer
you should edit your original question and add additional info there. perhaps you misunderstand Christopher's reply, but he did give you mostly what you need to make it work, it just needs to be hooked differently if you want it in your theme's functions.php and not as a plugin. see this answer I gave to a similar question. – Milo Aug 24 '11 at 17:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.