Need help regarding wordpress's wp_rewrite function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rahulsonar
Forum Newbie
Posts: 10
Joined: Wed Mar 11, 2009 8:54 am

Need help regarding wordpress's wp_rewrite function

Post by rahulsonar »

Hello,

I have installed 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_Ref ... WP_Rewrite, but didnt work.. Can anyone help please ?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Need help regarding wordpress's wp_rewrite function

Post by greyhoundcode »

When you say you followed the tutorial, is the following code what you used?

Code: Select all

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');
 
// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
 
// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';
    return $newrules + $rules;
}
 
// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'id');
    return $vars;
}
If so, did you simply activate this as a plugin "as is", without further modification (obviously you would have added the plugin name, version and other data first)? Just trying to get a better idea of how you used the code.
Post Reply