Quick n’ Dirty Category RedirectionFiled Under: Snippets, plugins

This is post number nine in my series, Quick N Dirty plugins. This plugin is perhaps the simplest of all and is designed to illustrate the basic method of interceding in the template selection process by showing how you can use a categories to link between blogs that you own.
Before getting into the plugin itself it is probably a good idea to explain how the template process works in WordPress.
When WordPress has analysed the page being requested, used that to determine whether the page being requested is a single page, home page, category, etc it calls the template selection process. This process is a series of IF statements which test each page type, i.e. is_home(), and uses a PHP include statement to include the appropriate theme file.
Once the appropriate file is included the script is terminated so that the theme file and any code that is called within it, is the very last thing that runs.
The plugin I am demonstrating here uses the action, template_redirect. This is called immediately before the list of IF statements in the template selection process. This means that we can select our own template file, or run any other code we want, pre-empting the normal template selection process.
Here is the plugin, I will explain more below:
<?php /* Plugin Name: Quick n' Dirty Category Redirection Plugin URI: http://www.wp-fun.co.uk/2008/01/27/quick-n-dirty-category_redirection/ Description: Redirects requests for a specified category to an alternate url Author: Andrew Rickmann Version: 1 Author URI: http://www.wp-fun.co.uk */ //This is the line that adds your filter into the list. // 'template_redirect' is the name of the faction // 'qnd_category_redirection' is the name of the function, below add_action( 'template_redirect' , 'qnd_category_redirection' ); function qnd_category_redirection(){ //a standard is_category tag, as used within template files if ( is_category('photography') ) { //if the photography category is requested go to my other blog that contains my photos wp_redirect('http://www.arickmann.co.uk/collections/photography/'); //this exit is not needed because we are redirecting to another site //but, if we were including a file we would need this to terminate execution of the code exit; } } ?>
The execution of the plugin is fairly simple. It is called when the template process begins. First we check to see if the category we want to use is being requested. If it isn’t, the template process just runs on and selects a template page in the normal way.
If we are requesting the photography category then the script redirects the user to my other blog where I keep my photographs. This means that any request for the photography category on one blog, will send the user to that category on my other blog.
I have also included the exit command to illustrate its importance in other circumstances. It isn’t needed in this case because the script will redirect the user before the exit command is reached but, if we were going to include an alternative template page then it would be essential.
For example, you could replace the code inside the function with the following to include a custom single post page for pages in the photography category:
if ( is_single() && in_category('photography') ) { if ( file_exists( TEMPLATEPATH . '/single-photography.php' ) ) { include( TEMPLATEPATH . '/single-photography.php' ); exit; } }
If the exit were not included then the WordPress template process would still be running and would include the normal single.php file from the template, or, if there was none, the index.php file. You would have two pages loaded on screen at once, albeit featuring the same content.
Hopefully you can see from this just how easy it is to add additional template file options.
Note: If you copy the content of this plugin you will need to replace all the quote marks as WordPress replaces them with fancy ones.
- Permalink
- Andrew Rickmann
- 2 Feb 2008 12:13 AM
- Comments (0)