Using the template_redirect hook for custom WordPress URIs
Posted on May 14, 2010. Updated May 25, 2010.
Summary
Add "fake" pages to your site with these quick additions to your Wordpress or Wordpress Mu theme code, taking advantage of the 'template_redirect' plugin hook.
In developing a large-scale WordPress Mu site, I hit a point where I needed to create custom pages and associated templates on each blog. Rather than iterating through each blog, I simply added a "fake" page to my WordPress theme code, applying it as a new feature to each site in my WPMU-based network.
In this example, I'll show you how to add a links page to your site, without actually creating a page in the WordPress admin interface.
First, be sure you have a file called links.php in your theme's template directory.
<?php
/*
Template Name: Links
*/
?>
<?php get_header(); ?>
<h2>My example links page</h2>
<p>This is an example list of folks that I know.</p>
<?php
$bm = get_bookmarks( array(
'orderby' => 'rand',
'order' => 'ASC',
'limit' => -1,
'hide_invisible' => 1,
'show_updated' => 0,
'include' => null,
'exclude' => null,
'search' => '.'));
?>
<?php foreach ($bm as $bookmark): ?>
<p><?php echo $bookmark->link_name ?> - <?php echo $bookmark->link_url?></p>
<?php endforeach; ?>
<?php get_footer(); ?>Next, include this get_uri() function in your functions.php file:
function get_uri() {
$request_uri = $_SERVER['REQUEST_URI'];
// for consistency, check to see if trailing slash exists in URI request
if (substr($request_uri, -1)!="/") {
$request_uri = $request_uri."/";
}
preg_match_all('#[^/]+/#', $request_uri, $matches);
// could've used explode() above, but this is more consistent across varied WP installs
$uri = $matches[0];
return $uri;
}Add this set of functions and wordpress actions in your functions.php file:
function links_template_title() {
return "My Example Link Page";
}
function links_template() {
$uri = get_uri();
if ($uri[0]=='links/') {
add_filter('wp_title', 'links_template_title', 20);
// optional hook for the_title
// add_filter('the_title', 'links_template_title', 20);
include(TEMPLATEPATH . '/links.php');
exit;
}
}Now go to www.myexample.com/wordpress/links and enjoy your custom links page!
To take it a bit further, we could add another custom page with a few modifications, such as a local weather page. Just create another custom page template, page-weather.php and put it in your theme directory.
<?php
/*
Template Name: Weather Template
*/
?>
<?php get_header(); ?>
<h2>My example weather page</h2>
<p>This page could pull some information from the national weather service.</p>
<p>Forecast: The rain in Spain falls mainly on the plain.</p>
<?php get_footer(); ?>Add this to your functions.php file:
function weather_template_title() {
return "Weather";
}
function weather_template() {
$uri = get_uri();
if ($uri[0]=='weather/') {
add_filter('wp_title', 'weather_template_title', 20);
// add_filter('the_title', 'weather_template_title', 20);
include(TEMPLATEPATH . '/page-weather.php');
exit;
}
}Now go to www.myexample.com/wordpress/weather and enjoy your custom weather page.
This entry was posted on Friday, May 14th, 2010 at 3:54 pm and is filed under Developer Notes. You can follow any responses to this entry through the RSS 2.0 feed.Both comments and pings are currently closed.