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

February 2, 2008

Quick N Dirty

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:

[php]
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.

Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Fun With Minimalism
Sidebartabs plugin?
no comment
page 248
Write One Plugin Without Repetition Deviation Or Hesitation
Saveadminoptions wordpress?
2 comments
page 27
Updating Code Snippets Here
Wordpress fun themes?
no comment
page 1338
Using Your Own Url Shortener
Htaccess shorten url?
4 comments
page 1190
Wordpress Chat
Wordpress chat?
3 comments
page 1308
Charcoal Theme Available For Wordpress
Simple charcoal wordpress themes?
2 comments
page 959
New Tabbed Widget Plugin
Tabbedwidget on wp 29?
no comment
page 333
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Fun With Sidebar Tabs Styling
Image under sidebar css wordpress?
2 comments
page 336
Post Image The Easy Peasy Way
Wordpress image link anchor tag?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Wpunlimited The Ultimate Wordpress Theme
Wordpress html customisation?
3 comments
page 1141
Dont Mess With My Toot Toot
Wordpress create new post type?
16 comments
page 599
Wordpress Chat
Wordpress chat themes?
3 comments
page 1308
Using Your Own Url Shortener
Url shortener?
4 comments
page 1190
Quick N Dirty Bookmark Navigation
Dirty bookmarking links?
no comment
page 127
Categories Vs Tags Either Neither Or Both
Both either neither image?
12 comments
page 7
My Experience Of Flexx
Flexx theme review?
4 comments
page 1026
Premium Ithemes Review Photo Gallery
Ithemes reviews?
4 comments
page 226
3 Ways To Speed Up Your Blog Without A Cache Plugin
Comment on speed up your blog?
one comment
page 1321
Wordpress 25 Exif Fields
Wordpress exif plugin?
12 comments
page 230
Adding Settings To Admin Pages
Wordpress addsettingsfield?
3 comments
page 793
Updating Code Snippets Here
Fun wordpress plugins?
no comment
page 1338
How To Add Sidebars To A Theme
How to register two sidebars in wp?
10 comments
page 1053
Html 5 Gallery
Html5 image gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress theimage?
26 comments
page 1065
Using Your Own Url Shortener
Short url?
4 comments
page 1190
Using Wordpress As A Php Framework
Php framework wordpress?
2 comments
page 335
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Wordpress Vs Graffiti
Html5 cms?
8 comments
page 95
Post Image The Easy Peasy Way
Wordpress medium size image link?
26 comments
page 1065
  1 query every 1567 seconds, updated 1 seconds ago.
Post a comment?