Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

Any ideas on how I can correct this behaviour?

Cheers!

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.

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

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? –  nness 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

 
discard

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.