Creating Custom URLs

Posted: 24th Dec 2007, in: Snippets, plugins - Older Post - Newer Post

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.

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.

 

Comments

  1. 1

    Atoms (http://tups.lv) commented at 10:02 pm, 3rd 03 2008:

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

     
  2. 2

    Andrew Rickmann (http://www.arickmann.co.uk) commented at 7:45 am, 4th 03 2008:

    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.

     

Leave a Reply

I am currently testing a comment link policy which means commenters do not get a link. There is a poll, and open comments for feedback on the comment policy page.

Please note. I am currently using an experimental antispam technique on this blog. If you run into problems please let me know using the Get in Touch link at the top of the page. Thanks, Andy.

Subscribe without commenting

Feed Icon - Get fed with RSS