Custom WordPress Pages and Canonical Links
WordPress is not just a blogging platform. With some work, you can turn it out into a full working CMS. The simplest and most powerful way to extend it is through Custom Pages. As an example, my torrent site vgmTorrents is based on WordPress and makes wide use of custom templates.
How to create a custom page
Creating a custom page and integrating it in your layout is very simple. Just go in your theme’s folder and create a new PHP script, better using a prefix in the filename to avoid conflicts with other files.
Once you have this empty file, add these lines in it:
/*
Template Name: MyPageName
*/
if (__FILE__ == $_SERVER[‘SCRIPT_FILENAME’]) { die(); }
get_header();
get_footer();
?>
Change MyPageName to any name you want for your page: WordPress will use it to refer to your page in the future. That if statement will prevent direct access to the script.
get_header and get_footer will display your theme’s header and footer, so any content output between those two calls will be wrapped in your blog layout.
In this script you have access to all WP functions, plugins functions and template tags, so you can do pretty much anything in it.
After you’ve saved this file, create a normal WordPress page in your wp-admin area, but before publishing it make sure you’ve selected the template you just created using the Template dropdown menu in the Attributes section (right side). That’s it.
Canonical URLs
Suppose your custom template serves multiple pages by accepting an id parameter and loading different content based on it. If you use a plugin that automatically adds canonical URLs to your pages, such as All in One SEO Pack, you may want to prevent those pages from having the same canonical URL and never getting indexed by search engines.
The solution is to disable All in One SEO Pack on that page and add the canonical URL on our own. Just enable the “Disable on this page/post” checkbox in your page and add those lines in your template script, before the get_header call.
// $id should be the id passed as a parameter to our page
$canonical = get_bloginfo(‘wpurl’).‘/mypagename/?id=’.$id;
?>
In your theme’s header file, add this in your <head> tag:
<link rel="canonical" href="<?php echo $canonical ?>" />
<?php endif ?>
Our pages will now have different canonical URLs and will all get indexed by any search engine.
Recent Comments