888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

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:

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.

Improve Your Typography With Plugins
Wordpress typography plugin?
one comment
page 721
Quick N Dirty Replacement Text
Dirty replacement?
no comment
page 122
How To Add Sidebars To A Theme
How to add pages in footer wordpress?
11 comments
page 1053
Using Your Own Url Shortener
Tiny urls htaccess?
4 comments
page 1190
Post Image The Easy Peasy Way
How to post all gallery images in one post wordpress?
26 comments
page 1065
Post Image The Easy Peasy Way
Get attachment by post?
26 comments
page 1065
Html 5 Gallery
Html 5 tab?
6 comments
page 1305
Wordpress 25 Exif Fields
Exif info display wordpress?
12 comments
page 230
Six Million Ways To Die Choose One
6 million ways to die so i chose?
14 comments
page 1128
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
Fun With Sidebar Tabs Styling
Funwithsidebartabs customize css?
2 comments
page 336
My Experience Of Flexx
Flexx theme review?
4 comments
page 1026
Wpunlimited The Ultimate Wordpress Theme
Ultimate admin theme?
3 comments
page 1141
Quick N Dirty Admin Login Screen
Css login screen?
no comment
page 128
Html 5 Gallery
Html 5 e wordpress?
6 comments
page 1305
Dont Mess With My Toot Toot
Toot html5?
16 comments
page 599
Fun With Sidebar Tabs Styling
Css tabcontentcontainer?
2 comments
page 336
Using Your Own Url Shortener
How to create your own url shortener?
4 comments
page 1190
Dont Mess With My Toot Toot
Wordpress custom content types?
16 comments
page 599
Using Your Own Url Shortener
Funny url shortener?
4 comments
page 1190
Theming Habari Vs Wordpress
How to create a habari theme?
13 comments
page 440
Post Image The Easy Peasy Way
Addd multiple images to post wordpress?
26 comments
page 1065
Upload From Url
Upload by url?
6 comments
page 326
Html 5 Gallery
Html5 image gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Add image url to page data wordpress?
26 comments
page 1065
Wpunlimited The Ultimate Wordpress Theme
Html 5 photo gallery?
3 comments
page 1141
Html 5 Gallery
Image galleries in html5?
6 comments
page 1305
Six Million Ways To Die Choose One
Six million ways to die?
14 comments
page 1128
Html 5 Gallery
Html 5 foto gallery?
6 comments
page 1305
Charcoal Theme Available For Wordpress
Charcoal theme?
2 comments
page 959
Wpunlimited The Ultimate Wordpress Theme
Html 5 photo gallery?
3 comments
page 1141
Why I Ditched Disqus
Disqus theme?
5 comments
page 1175
Why I Ditched Disqus
Disqus theme?
5 comments
page 1175
  1 query every 1171 seconds, updated 1 seconds ago.
Saturday, 12am
 __
(__)
   `

 admin

PS: this post needs to be fixed, syntax-highlighting is temporarily broken.

Thursday, 7am
 __
(__)
   `

 Martin

Does not work for me, the new generated rules does not override the old ones.

As example :

i got myblog.com/new-model-toilet-seat but if a want to get
myblog.com/mycustomurl/new-model-toilet-seat it redirects me directly to myblog.com/new-model-toilet-seat , wich had a diferent content that myblog.com/mycustomurl/new-model-toilet-seat

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 :/