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 am trying to create a custom Rewrite URL something like

function add_my_rule() {    
    global $wp; 
    $wp->add_query_var('state');   
    $wp->add_query_var('state_destination');
    add_rewrite_rule('destination/([0-9]+)/([^/]*)/page/([0-9]+)','index.php?pagename=destination&state=$matches[1]&state_destination=$matches[2]&paged=$matches[3]','top');
    add_rewrite_rule('destination/([0-9]+)/([^/]*)','index.php?pagename=destination&state=$matches[1]&state_destination=$matches[2]','top');
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action('init', 'add_my_rule');

Idea is to rewrite all URL which have following pattern

www.abc.com/destination/state/state-destination

I have a custom template destination.php and i want all such URL's to be handled there.I have done same in past and that is working fine but now it does not seems working at all as for the given URL

http://localhost/xxx/destination/himachal-pradesh/dalhousie

it always going to single.php and not to destination.php.I tried to flush the cache and even browser one but seems like either my rewrite Rule is not correct or there is some other issue. Any help in this will help me to move ahead.

As an added information my current URL-Structure in WordPress is /%year%/%monthnum%/%postname%.html i am using WAMP Server with mode-rewrite enable.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

this is a nice little re-write class created by: Author Kyle E Gentile I found this a while back which works a treat in most plugins I've created...

Save the first section of code in a file called: add_rewrite_rules.php

<?php
/*
//Author Kyle E Gentile
//To use this class you must first include the file.  
//After including the file, you need to create an options array.  For example:
$options = array(
    'query_vars' => array('var1', 'var2'),
    'rules' => array('(.+?)/(.+?)/(.+?)/?$' => 'index.php?pagename=$matches[1]&var1=$matches[2]&var2=$matches[3]')
);
//After creating our $option array, 
//we will need to create a new instance of the class as below:
$rewrite = new Add_rewrite_rules($options);
//You must pass the options array, this way. (If you don't there could be problems) 
//Then you can call the filters and action functions as below:
add_action('wp_head', array(&$rewrite, 'flush_rules'));
add_action( 'generate_rewrite_rules', array(&$rewrite, 'add_rewrite_rules') );
add_filter( 'query_vars', array(&$rewrite, 'add_query_vars') );
//That is it.
*/

//prevent duplicate loading of the class if you are using this in multiply plugins
if(!class_exists('add_rewrite_rules')){

    class Add_rewrite_rules{

        var $query_vars;
        var $rules;

        function __construct($options){
            $this->init($options);
        }

        function init($options){
            foreach($options as $key => $value){
                $this->$key = $value;
            }
        }

        function rules_exist(){
            global $wp_rewrite;

            $has_rules = TRUE;

            foreach($this->rules as $key => $value){
                if(!in_array($value, $wp_rewrite->rules)){
                    $has_rules = FALSE;
                }   
            }

            return $has_rules;
        }

        //to be used add_action with the hook 'wp_head'
        //flushing rewrite rules is labor intense so we better test to see if our rules exist first
        //if the rules don't exist flush its like after a night of drinking  
        function flush_rules(){
            global $wp_rewrite;

            if(!$this->rules_exist()){
                //echo "flushed"; // If want to see this in action uncomment this line and remove this text and you will see it flushed before your eyes
                $wp_rewrite->flush_rules();
            }
        }

        //filter function to be used with add_filter() with the hook "query_vars"
        function add_query_vars($query_vars){

            foreach($this->query_vars as $var){
                $query_vars[] = $var;
            }

            return $query_vars;
        }

        //to be used with a the add_action() with the hook "generate_rewrite_rules"
        function add_rewrite_rules(){
            global $wp_rewrite;

            $wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
        }

    }

}
?>

Then include the file in your code to generate your new Re-Write rules..

<?php
//-------------------------------------------------
//ADDING REWRITE RULES AND QUERY VARS
//-------------------------------------------------
include('add_rewrite_rules.php');
$options = array(
    'query_vars' => array('state', 'state_destination'),
    'rules' => 
        array(
            '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?  pagename=$matches[1]&state=$matches[2]&state_destination=$matches[3]'
        )
);
    $rewrite = new Add_rewrite_rules($options);
    add_action('wp_head', array(&$rewrite, 'flush_rules'));
    add_action('generate_rewrite_rules', array(&$rewrite, 'add_rewrite_rules') );
    add_filter('query_vars', array(&$rewrite, 'add_query_vars') );
   //-------------------------------------------------
   //ADDING REWRITE RULES AND QUERY VARS
   //-------------------------------------------------
   ?>

that should be it, like I said I've used this a few times while building different plugins, I hope it works ok for you..

Marty

share|improve this answer
    
I am confused about the rule, will it work for /destination/ as your rewrite rule seems not taking care of the URL i have specified or am i missing something? –  Umesh Awasthi Oct 28 '12 at 11:46
    
yes, destination is the top level page, state & state_destination are the queries used on this page, so if you use a custom template on the page='destination' write a small piece of code to check and see if its being picked up.. ie: visit yoursite.com/destination/?state=whatever&state_destination=somethingelse you should (if your permalinks are set to /%postname%/) see your url change to /destination/state/state-destination/ then in your code use something like: if(isset($_GET('state')){ echo $_GET['state']; } this should print out the query var passed to that page. –  Marty Oct 28 '12 at 11:54
    
i will try and update about this, on a curious note any idea why my solution is not working? –  Umesh Awasthi Oct 28 '12 at 12:02
    
I dont sorry. have you tested to see if 1 param is working, ie: take out the 2nd param, and test that, once thats working Id try to get the 2nd working. Marty –  Marty Oct 28 '12 at 12:05
    
i tested your code, its not working as expected http://localhost/xxx/destination/himachal-pradesh/dalhousie becoming http://localhost/xxx/destinations while i want that URL should remain same and i have created a page destination.php, this page should be used to display information about them. –  Umesh Awasthi Oct 28 '12 at 12:08
show 3 more comments

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.