WordPress custom URL Rewrites redirecting not masking

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;
    $wp_rewrite->flush_rules();
}
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/.

Read More

Any ideas on how I can correct this behaviour?

Cheers!

Related posts

Leave a Reply

1 comment

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

    <?php
    /**
     * 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()
        {
            add_rewrite_rule( 
                 "{$this->file_name}.php$"
                ,"index.php?{$this->query_arg}={$this->query_var}"
                ,'top' 
            );
        }
    
    
        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";
                exit();
            }
            return;
        }
    }
    

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