WordPress Include Tags

The WordPress documentation on Include Tags listed four different functions which template developers can use to in their templates:

  1. <?php get_header(); ?>
  2. <?php get_footer(); ?>
  3. <?php get_sidebar(); ?>
  4. <?php comments_template(); ?>

Most templates in the WordPress themes directory use these set of functions, simply because they are used with the default Kubrick template, and according to the documentation, these functions provide the additional functionality over a regular PHP include like <?php include (TEMPLATEPATH . '/header.php'); ?>, because if the the file required (e.g. header.php) is not found in the template’s directory, the default template’s header.php will be used instead.

However, I figured out that there might be a reason why a developer may choose NOT to use these functions.

I don’t know how exactly to put the challenge I faced into a statement… but here is the scenario which I found and issue with.

In my template, I normally use an extra config.php file, where I define a few variables, like my FeedBurner Feed Address, or the main meta keywords for my blog. As I did so, I used a <?php include (TEMPLATEPATH . '/config.php'); ?> at the top of every main page I use (i.e. index.php, single.php, archive.php and page.php) so that the variables will be available for use in that page.

And in these main pages are where I used to have get_header(), get_footer(), get_sidebar() and comments_template().

So, for example, in my sidebar page (sidebar.php), I have an RSS icon which is hyperlinked to my blog feeds – which I want to point to my Feedburner feed address. However, it seems that the variables in scope on the main page is not available in these pages.

Even when I did another <?php include (TEMPLATEPATH . '/config.php'); ?> in my sidebar.php, the variables still showed blank when I loaded the page on the browser.

When I swapped <?php get_sidebar(); ?> with <?php include (TEMPLATEPATH . '/sidebar.php'); ?>, the variables worked well.

I think the four include functions by WordPress works well for templates that do not do a lot of customization and optimization. They work perfectly for themes like the default Kubrick. However, if you are a developer who wants to tweak the details of your template, you might be better off using regular PHP functions, where you are assured of the exact functionality (of course, unless you have the technical expertise to look into the WordPress source files).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>