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?


1 Answer 1


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.

  • 1
    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? Aug 29, 2011 at 7:46
  • Give it a try and comment out/uncomment part by part. You'll see :)
    – kaiser
    Aug 29, 2011 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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