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.

Wordpress Chat
Wordpress chat?
3 comments
page 1308
Post Image The Easy Peasy Way
How to receive an image using post php?
26 comments
page 1065
Fun With Sidebar Tabs Styling
Tabs sidebar box?
2 comments
page 336
Post Image The Easy Peasy Way
Wordpress attachment url?
26 comments
page 1065
How To Add Sidebars To A Theme
How to register sidebar in wordpress?
10 comments
page 1053
Fun With Sidebar Tabs Styling
Alternate css in wp sidebar widgets?
2 comments
page 336
Using Your Own Url Shortener
Short url?
4 comments
page 1190
Post Image The Easy Peasy Way
Attachment picture in comments?
26 comments
page 1065
Doing More With Widgets Changing Layouts
Registersidebarwidget thematic?
6 comments
page 28
Using Your Own Url Shortener
Short links wordpress htaccess?
4 comments
page 1190
Updating Code Snippets Here
Fun wordpress sites?
no comment
page 1338
Dont Mess With My Toot Toot
Fun with toots?
15 comments
page 599
Dont Mess With My Toot Toot
Fun with toots?
15 comments
page 599
Post Image The Easy Peasy Way
Wordpress hook image size?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp image get link of attachment?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp get post attchements?
26 comments
page 1065
Html 5 Gallery
Html5 wordpress theme?
6 comments
page 1305
Photoshop Design Framework
Photoshop framework?
3 comments
page 296
Updating Code Snippets Here
Fun with wordpress?
no comment
page 1338
Post Image The Easy Peasy Way
Find the one we want based on its characteristics?
26 comments
page 1065
Why I Ditched Disqus
Disqus limitations?
5 comments
page 1175
Quick N Dirty Admin Login Screen
Login page css?
no comment
page 128
Quick N Dirty Admin Login Screen
Login page css style?
no comment
page 128
Wp Polls Reviewed
Different templates in wppolls?
one comment
page 58
Post Image The Easy Peasy Way
Php code add attachment wordpress?
26 comments
page 1065
Wordpress Chat
Chat para wordpress?
3 comments
page 1308
Photoshop Design Framework
Framework to photoshop?
3 comments
page 296
Post Image The Easy Peasy Way
Php get attachment from post?
26 comments
page 1065
Are Child Themes The Best Option
Wordpress thematic html5 child theme?
15 comments
page 1262
Are Child Themes The Best Option
Wordpress thematic html5?
15 comments
page 1262
Why I Ditched Disqus
Better than disqus?
5 comments
page 1175
Post Image The Easy Peasy Way
Wordpress imaged default url?
26 comments
page 1065
Are Child Themes The Best Option
Wordpress thematic html5?
15 comments
page 1262
  1 query every 1067 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 :/