Need help regarding wordpress's wp_rewrite function

Discussion in 'PHP' started by MayurGondaliya, Feb 26, 2010.

  1. #1
    Hello,

    I know this is a CMS question, but I think this is more related to programming..
    I am using wordpress on my domain and I have a page called recent-coupons, which loads like mentioned below:

    http://mydomain.com/recent-coupons/

    I use query string in this page for city, like:

    http://mydomain.com/recent-coupons/?city=new-york

    and then I display the list of coupons in new york city. Now, I want to rewrite this page as follows:

    http://mydomain.com/recent-coupons/new-york/

    I tried the tutorial at http://codex.wordpress.org/Function_Reference/WP_Rewrite, but didnt work.. Can anyone help ?
     
    MayurGondaliya, Feb 26, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hey,

    where did you put your code for the rewrite rule? You have the write a plugin which adds your desired rewrite rule to the wp rewrite rules array.

    Here is a little example what your code should look like:

    
    <?php
    function recent_coupons_rewrite($wp_rewrite) {
    	$recent_coupons_rules = array(
            'recent-coupons/(.+)/?$' => 'recent-coupons/?city=' . $wp_rewrite->preg_index(1)
        );
    	
        $wp_rewrite->rules = recent_coupons_rules + $wp_rewrite->rules;
    }
    
    function add_recent_coupons_query_vars($public_query_vars) {
    	
    	$public_query_vars[] = "name";
    	
    	return $public_query_vars;
    }
    
    add_filter('generate_rewrite_rules', 'recent_coupons_rewrite');
    add_filter('query_vars', 'add_recent_coupons_query_vars');
    ?>
    
    PHP:
    I've not tested it but i use this for a similar operation on my wp test site.
     
    Nyu, Feb 26, 2010 IP