April 15, 2010

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.

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:

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.

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:

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 title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Wpgetattachmentimagesrc size?
24 comments on page 1065

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065
  every 1744s, 1s ago, in 0.02s.
 __
(__)
   `
 Michael

Oh. I SO want to know how to do this custom URL information for /video/ or /articles/ based on post template type. But alas, I do not understand anything you said.
I’d love if you could write a companion post that showed an example for dummies like me. I’m a designer, not a programmer.

 __
(__)
   `
 Brad

I’m NO wp_rewrite expert but shouldn’t it be admin_init vs init so that the rule is not re-created at every page load and only re-created when the admin page is loaded?

 __
(__)
   `
 admin

OK, fixed the code snips.

 __
(__)
   `
 admin

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

 __
(__)
   `
 Liens du 24/11/2008

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

 __
(__)
   `
 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 [...]

 __
(__)
   `
 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.

 __
(__)
   `
 Atoms

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


0.01s