Ferodynamics Network

popular: profile privacy, mobile privacy

December 24, 2007

It was pretty interesting to look back over a plugin I wrote, and thought was pretty good, more than a year ago. This was before I started considering releasing plugins properly and therefore taking steps to become proficient. It really highlighted to me how far I had come. One of things that I found was that to create a custom URL I had disassembled the way WordPress does it internally, and copied that. I completely missed the right way of doing it. So that is what I am going to explain here.

Note, I assume that you are familiar with writing plugins in general.

First, what do I mean by custom URLs?

Look at the address used by my tool, Fun with Plugins: http://www.wp-fun.co.uk/wizzards/fun-with-plugins/

What this URL does is to convert that to http://www.wp-fun.co.uk?wizzards=fun-with-plugins. This is used by WordPress to compile a list of variables that are made available through the WP_Query object.

I promise it will make a bit more sense in a minute.

So, lets say you wanted to create a photoblog. You want the URL for each image to be YOURBLOG/image/name and you will use a custom page in your template to display them: images.php.

In your plugin you will need four hooks:

  • init
  • generate_rewrite_rules
  • query_vars
  • template_redirect

When init is called you will need to flush the rewrite rules. The rewrite rules tell WordPress what to do with each part of the URL. Flushing them tells WordPress to recalculate those rules.

[php]
add_action('init', 'flush_rewrite_rules');

function flush_rewrite_rules() {
global $wp_rewrite;

$wp_rewrite->flush_rules();
}

When the rules are flushed WordPress will recalculate them. When it does that it will call the action – generate_rewrite_rules giving us the opportunity to add a new one:

[php]
add_action('generate_rewrite_rules', 'add_rewrite_rules');

function add_rewrite_rules( $wp_rewrite ) {
$new_rules = array(
'image/(.+)' => 'index.php?image=' .
$wp_rewrite->preg_index(1) );

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

Next we need to tell WordPress to gather the image variable so we can make use it through the query object.

[php]
add_filter('query_vars', 'add_query_vars');

function add_query_vars( $qvars ){
$qvars[] = 'image';
return $qvars;
}

Having done this the variable is available to check through $wp_query, like this:

[php]
add_action('template_redirect', array(&$this, 'template_redirect_intercept'));

function template_redirect_intercept(){
global $wp_query;
if ( $wp_query->get('image') ) {
if (file_exists( TEMPLATEPATH . '/images.php' )) {
include( TEMPLATEPATH . '/images.php' );
exit;
}
}
}

$wp_query->get(‘image’) will return false if the second part of the URL, the image name, hasn’t been entered or the image name if it has. You would use this return value in your custom page to figure out what it is you need to display there.

Why You Should Try Netbeans
6 comments
page 973
Wpunlimited The Ultimate Wordpress Theme
3 comments
page 1141
Auto Cycle Fun With Sidebar Tabs
one comment
page 1129
Beware Wp Cache
8 comments
page 1310
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress 25 Exif Fields
12 comments
page 230
Html 5 Gallery
6 comments
page 1305
Quick N Dirty Comment Stats
no comment
page 130
Divine Proportions
3 comments
page 145
Beware Wp Cache
8 comments
page 1310
Html 5 Gallery
6 comments
page 1305
Beware Wp Cache
8 comments
page 1310
Charcoal Theme Available For Wordpress
2 comments
page 959
Html 5 Gallery
6 comments
page 1305
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Quick N Dirty Admin Login Screen
no comment
page 128
Wordpress Chat
one comment
page 1308
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Silence Is Golden
3 comments
page 213
Questions About Habari For Wordpress Users
6 comments
page 424
Theming Habari Vs Wordpress
13 comments
page 440
My Experience Of Flexx
4 comments
page 1026
Plugin Update Fun With Photo Data 2
one comment
page 815
Post Image The Easy Peasy Way
26 comments
page 1065
Categories Vs Tags Either Neither Or Both
12 comments
page 7
Gaining Benefits From Plugins
8 comments
page 1167
Fun With Theme Widgets
24 comments
page 867
Beware Wp Cache
8 comments
page 1310
Six Million Ways To Die Choose One
14 comments
page 1128
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
  updated 1 seconds ago
Thursday, 2pm
Liens du 24/11/2008

[...] Creating Custom URLs : pour jouer avec la r

Thursday, 6pm
Don’t mess with my Toot Toot | WP FUN

[...] written before about the process of creating a custom URL but there are effectively three processes [...]

Tuesday, 7am
Andrew Rickmann

Atoms,

The principal doesn’t change, so I can’t think of any reason why it wouldn’t work.

Provided the path is correct in both locations, i.e. the file check and the include statement, then it should work.

My usual technique is to insert an echo statement before the include statement, and each time it doesn’t appear move it out a level, i.e. out of the IF statement, or out of the function and into the calling function until it appears. Once it does you have found the last point that runs properly and can start fixing from there.

Monday, 10pm
Atoms

And if my template is in plugin directory ? i tried to change the path but nothing changes :/