WordPress Page Specific Hooks

There are many hooks in WordPress for plugin developers to catch as an extension point, downside of it is those hooks will be raised on EVERY PAGE, which is an issue if you need to use a large chunk of javascript only for one page, those hooks called actions and filters are site wide, if not careful large chunk of javascript meant for one page could be emitted to every page, a definitely performance killer.

As a responsible plugin developer, you would normally add a condition check in plugin hook handlers before you print anything out, without this condition check, script or style sheet print will be on every page.

// add wp scripts
add_action('wp_print_scripts', 'yourheadscriptshandler');

function yourheadscriptshandler (){
  if (is_page(‘yourpagetitle'))

The above code shows a very traditional way to use the jquery library ONLY for page with title as ‘yourpagetitle’. It is unlikely an ideal solution for cases like this as for plugin developers they have no idea which page you are going to use their plugin.

This triggers me into searching other ways to solve this problem, like hooks that are page specific. Unfortunate that there is nothing like a page specific hook, unless you know how to program or customize WordPress at template or theme level.

Plugins are meant for site wide, for example, do something to every page or every post, when you only want to do something for a particular page, there is another kind of WordPress customization: custom page template.

Custom page template is not theme development, but allows you to tweak a particular page without affecting other part of your site. And the code in custom page template only kicks in when the page which is using this template is called.

How to create a custom page template:

As the starting point, copy an existing template from wp-content/themes/yourtheme/ and placed it under same folder

Template Name: My Template 

<?php get_header(); ?>

<div id="content">

<?php do_action('myaction'); ?>


<?php get_footer(); ?>

Here is a simple example of custom page template, then create a new page, on the page edit page, there is an area called – page attributes-template, you will see My Template is magically under the template drop down, if you do not use a custom page template, then WordPress would use system template on your new page.

A couple of points of a custom page template:

1. There must be Template Name entry in the comments area which will be extracted by WordPress as the name of this page template

2. It can be shared by multiple pages

3. You can register page specific actions(myaction in example) in these templates , and hook up these actions in your plugin without worrying them being called every time. An example of hooking up custom action –myaction

add_action('myaction', 'myactionhandlerexample');

function myactionhandlerexample()
   echo 'Hello World';

Sum up:

WordPress plugin hooks are triggered in every page life cycle, do not print scripts or contents unconditionally, some time a custom page template might do a better job than a plugin.


This entry was posted on Wednesday, February 9th, 2011 at 2:25 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply