logo
  • Home
  • About
  • Plugins

Subscribe to Articles

Creating Custom URLs

Author: andrew Category: Uncategorized Tags: plugins, Snippets

Monday
Dec 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.

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.


Share:
image image image image image

Comments

Atoms (http://tups.lv)

March 3rd, 2008 at 10:02 pm

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

Andrew Rickmann (http://www.arickmann.co.uk)

March 4th, 2008 at 7:45 am

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.


Making best use of your categories
Re-Tooling

Recommended

WP Remix Banner

Archives

  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007

Tags

2.7 2.8 Admin Advertising Air Blogging blogs Cache Cliche Coding Comments Content Types CSS Curry Death development Disqus Features Habari image Interview Licensing Modes Monday Poll New Features Organisation Personal Platform Plugin Plugin-Practices plugins Plugin update Poll premium Readers Reviews Search Simplification Snippets Spam Themery Themes tools User Interface Widgets WLTC

Copyright 2009 Fun with WordPress - All Rights reserved.

Wordpress theme by: WPUnlimited