Rewrite Rules in WP2.0.x

Saturday, March 11th, 2006

OK, so I thought I’d write a littlle WP plugin tutorial for those of you that want to use rewrites when you create your own plugins b/c when I updated F Gallery I had a really tough time trying to get it all to work, mainly b/c WP 2.0 works differently than 1.5 did. So I thought that I’d save future developers time by offering them a some insight into how to do with for WP 2.0.1 or greater.

My only disclaimer is that I created F Gallery to work with WP 2.0.1 so I take no responsibility if the good people at WP change some code in future versions.

Ok, to start off, you’re gonna need to know how to create the rewrite rules array. The way it works is that the string you’re looking for is the name of the array variable and it’s value is the rule. I won’t be explaining how the variables work in the rewrite b/c I’m not an expect on them, I only know the basics. So my rules are as follows (FYI, $dir is directory that F Gallery is being used in, in my case it’s “photos”):


function fgallery_get_rewrite_rules() {

   $data["$dir/albums/?$"] = ‘index.php?pagename=’.$dir.’&view=albums’;

   $data["$dir/albums/([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&album=$matches[1]‘;

   $data["$dir/albums/([0-9]+)/([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&photo=$matches[2]&album=$matches[1]‘;

   $data["$dir/albums/([0-9]+)/page([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&album=$matches[1]&thumb_page=$matches[2]‘;

   $data["$dir/tags/?$"] = ‘index.php?pagename=’.$dir.’&view=tags’;

   $data["$dir/tags/([_0-9a-z-]+)/?$”] = ‘index.php?pagename=’.$dir.’&tags=$matches[1]‘;

   $data["$dir/tags/([_0-9a-z-]+)/([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&photo=$matches[2]&tags=$matches[1]‘;

   $data["$dir/tags/([_0-9a-z-]+)/page([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&tags=$matches[1]&thumb_page=$matches[2]‘;

   $data["$dir/page([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&thumb_page=$matches[1]‘;

   $data["$dir/([_0-9a-z-]+)/?$”] = ‘index.php?pagename=’.$dir.’&view=$matches[1]‘;

   $data["$dir/([_0-9a-z-]+)/page([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&view=$matches[1]&thumb_page=$matches[2]‘;

   $data["$dir/([_0-9a-z-]+)/([0-9]+)/?$”] = ‘index.php?pagename=’.$dir.’&view=$matches[1]&photo=$matches[2]‘;

}

The one thing to note is that this is different from WP 1.5 b/c in 1.5 WP dealt with the .htaccess and mod rewrite, but now in the new and improved WP, it’s all done under the hood, so to speak. So the long story is that with the old version of WP you’d use “$1″ to catch the first variable in the rewrite rules, now you use “$match[1]“.

Ok, now you have your rules, now you need to add them to the rest of the rules. First thing you need to do is get the rewrite rules with one of WP’s many hooks. In this case there are a couple hooks you can use to catch rewrites, but I use “generate_rewrite_rules” to call a function whenever WP tries to create the rewrite rules. So my function call looks like:

add_filter(’generate_rewrite_rules’, ‘fgallery_create_rewrite_rules’);

So now I have to create the function “fgallery_create_rewrite_rules”. So the important thing to note is that the function is passed the wp_rewrite variable. So my function looks something like:

function fgallery_create_rewrite_rules($wp_rewrite){

   $new_rules = $fgallery_get_rewrite_rules();

   $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;

}

Now that you’ve added your rules to WP, how do you access them? In the old version you used to use the $_GET variable in PHP to access them, but since WP is no longer using mod rewrites, the variable aren’t there, they’re now internal. They’re now in new array that stores the query variables. To access this variable you need the “query_vars” hook, which will pass your function the array of query variables that it’s looking for. This is key b/c if you don’t tell it which variables to get out of the rewrite then it won’t store them for you.

function fgallery_query_vars ($vars){

   $vars[] = ‘view’;

   $vars[] = ‘thumb_page’;

   $vars[] = ‘album’;

   $vars[] = ‘tags’;

   $vars[] = ‘photo’;

   return $vars;

}

add_filter(’query_vars’, ‘fgallery_query_vars’);

Ok so up to this point, we’ve created the rules, we’ve added our rules to WP and we’ve told WP store the variables, now how does one access those stored variables? Easy, you use the get_query_var(variable_name) function. and you replace the variable name with a string the represents the variable you’re trying to access. eg:

$view = get_query_var(’view’);

$thumb_page = get_query_var(’thumb_page’);

$album = get_query_var(’album’);

$tags = get_query_var(’tags’);

$photo = get_query_var(’photo’);

So that’s it, you now now how to add your variables to the rewrite and how to access them. It’s not that hard, but without any resources out there that tell you how to do it, it’s a bitch to try and figure out how to do it on your own.