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

I have the following code that exists in a plugin that is meant to take a request to the url /stores/directory/foorbar/ and pass foobar as a GET variable:

function pcrr_rewrite_rules($a) {
    $k = array('stores/directory/([^/]+)/?$' => 'index.php?pagename=directory&pod_store_id=$matches[1]');
    return $k + $a;
add_filter('rewrite_rules_array', 'pcrr_rewrite_rules');

# Flush existing rules and rewrite
function pcrr_activate() {
    global $wp_rewrite;
register_activation_hook(__FILE__, 'pcrr_activate');

The expected behaviour would be any requests to /stores/directory/foobar/ would instead load index.php?..., however, whilst requests are being handled, they are redirecting to the URL, instead of masking it. For example, a request to /stores/directory/foobar/ is redirected to /stores/directory/.

Any ideas on how I can correct this behaviour?


share|improve this question

migrated from stackoverflow.com Aug 29 '11 at 6:46

This question came from our site for professional and enthusiast programmers.

Quick'n'dirty Class to do this following below (you need to flush by hand in admin settings for permalinks):

 * Add query args & vars & redirect stuff somewhere else
 * Could be extended to support arrays of query args on input
 * @package Rewrite Class
 * @author F.J.Kaiser
class wpseAddRewriteRules
    var $file_name;
    var $query_arg;
    var $query_var;

    function __construct( file_name, query_arg, query_var )
        $this->file_name    = $file_name;
        $this->query_arg    = $query_arg;
        $this->query_var    = $query_var;

        add_action( 'wp_loaded', array( &$this, 'add_rewrite_rule') );
        add_filter( 'query_vars', array( &$this, 'add_query_vars') );
        add_action( 'parse_request', array( &$this, 'parse_request') );

    function add_rewrite_rule()

    function add_query_vars( $query_vars )
        $query_vars[] = $this->query_arg;
        return $query_vars;

    function parse_request( &$wp )
        if ( array_key_exists( $this->query_arg, $wp->query_vars ) ) {
            include plugin_dir_path( __FILE__ )."{$this->file_name}.php";

Note: Just written off the backside of my brain, so no waranty on anything. Typos & such may be hidden in there.

share|improve this answer
Looking through this, with the exception of the parse_request function, there isn't anything here particularly different from what I wrote. What prevents the URL from redirecting? – Lachlan McD. Aug 29 '11 at 7:46
Give it a try and comment out/uncomment part by part. You'll see :) – kaiser Aug 29 '11 at 8:25

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.