Adding Functionality to Templates
I recently saw a post in the forums for WordPress where someone was trying to add someone else’s php code to their themes. The code started off by setting a cookie. It was correctly commented in the source code to
…put this before any html
The reason being that to set a cookie, there must be no information sent to the browser yet. This is also true of other header(); php commands.
So, how can we put the script where we want, without having a whole bunch of errors thrown on the page?
The easiest (and most elegant) solution is to use php’s built in output buffering. Using output buffering means that memory is allocated for the output of the code and isn’t sent to the browser until you specifically tell it to do so.
The beautiful outcome is that you can get away with putting things in the ‘wrong’ order and get away with it.
For example, normally this code would break horribly:
<?php
echo 'Some text here';
header('Location: http://www.yahoo.com/');
?>
You’d get an error saying “headers already sent”
But the same code with output buffering works like a charm.
<?php
ob_start();
echo 'Some text here';
header('Location: http://www.yahoo.com/');
$contentStuff = ob_get_contents();
ob_end_clean();
echo $contentStuff;
?>
Now let’s apply this concept to themes as they are currently written for WordPress 1.5.1.2
No need to find a new theme, just open up index.php or possibly page.php
You’ll likely find code like this:
<?php get_header(); ?>
(the rest of the php code here)
<?php get_footer(); ?>
All you need to do is change it to
<?php
ob_start();
get_header(); ?>
(the rest of the php code here)
<?php get_footer();
$contentStuff = ob_get_contents();
ob_end_clean();
echo $contentStuff;
?>
So long as ob_start() is called before any output is sent the browser, adding php code to the sidebar, or any other part of your theme, shouldn’t cause any problems with headers.
Update: I just updated the Static Jack plugin to have output buffering enabled. So, even though you understand how to do this to your templates with hardcoding, you’ve now got the option to use output buffering instantly on ALL of your templates with the click of a button.
Enjoy.
2 Responses to “ Adding Functionality to Templates”
Leave a Reply
Syndicate
Categories
- Customer Service (1)
- Entrepreneurs (1)
- Marketing (3)
- Programming (9)
- Web Design (1)
- WordPress (5)




Rajan R says:
June 6th, 2005 at 2:02 pm
I can’t use StaticJack simply because I can’t mod_rewrite - I’m shared hosted. And the method you suggested above doesn’t work - the same error still crops out. This is how I did it:
…
As well as putting the remaining PHP code in sidebar.php. Doesn’t work. Nor does it work if I put the include_once before get_header(). Nor would it would if I put include_once in sidebar.php. Nor would it work putting the whole code on index.php
Administrator says:
June 7th, 2005 at 2:23 pm
Rajan,
I’m sorry that this solution didn’t work for you. It’s most likely an issue with your server setup, or something found in php.ini, which you won’t have access to in a shared hosting environment.
By the way, there are many shared hosting providers that will give you the ability to use mod_rewrite.
I suggest in your situation to move the code relating to cookies to the part of your theme that comes just before the first html is output. This might be in the header.php file, but will completely depend on the theme you’re using.