kieranbarnes do you know where your towel is?

Using Wordpress header and footers externally

Posted on September 24, 2008

I needed to integrate a Wordpress header (header.php) and footer (footer.php) into an external application.

There are quite a few ways round this, but none are really ideal. For example, if you want to include an external page in Wordpress for whatever reason, you could use the Exec-PHP plugin. Simple, hey? No.

What if that page does something a little bit tricky that can't be run through PHP's eval();?

I ran into these problems whilst integrating a 3rd party web application with a new Wordpress.
We needed both sites to look exactly the same, ideally, using the Wordpress theme as it creates all the page links etc for us.

How do we do it? Simple. I wrote a wrapper to download and save these two files so the 3rd party app can access them.

Here's my shell script, called update_headers.sh

#!/bin/bash
rm -rf header-wrapper.html && wget http://YOUR_URL/header-wrapper.php -O header-wrapper.html -q
rm -rf footer-wrapper.html && wget http://YOUR_URL/footer-wrapper.php -O footer-wrapper.html -q

It calls wget to download two simple files from the Wordpress directory.

header-wrapper.php looks like this

<?php
require('./wp-blog-header.php');
 
include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/new_theme/header.php";
?>

and footer-wrapper.php looks like this

<?php
require('./wp-blog-header.php');
 
include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/new_theme/footer.php";
?>

So, the script calls the header and footer wrapper, saves the file to the web root (or where ever you ran the script from, ready to be included by your 3rd party app.

This particular application uses the Smarty template engine, so all I needed to do was replace the appliaction's header code with the following line

{include file="/home/YOUR_PATH/header-wrapper.html"}

and of course, your footer will look similar.

Whenever the client updates the Wordpress pages, for example adds a new page etc, we just call the shell script. Or, if you're lazy, set it to run every hour from cron.

"A bit clunky", you say? Well you find a better way!

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Related posts:

  1. CubeCart and Wordpress Integration
    Following my 100% CubeCart orientated posts, here's another late night hack. It integrates a Wordpress...
  2. Custom Page Templates Not Showing in Wordpress
    I've recently starting getting an annoying error in Wordpress, I'm not sure if its my...
  3. Retro Fitting Record Pagination with ADOdb
    For my first project of the New Year I had to retro-fit pagination controls to...
  4. Removing Wordpress plugin references
    I'd like remove all references Wordpress plugins put into my HTML code. Am I selfish?...
  5. My best Wordpress plugins
    Here are a few of my Wordpress plugins that I can't do without. It is...

What this article useful to you?



Let me know, buy me a beer!
Alternatively, if you're feeling impecunious, you may like to subscribe to my RSS feed, or see other articles in the CubeCart, PHP, Wordpress category.

Comments (7) Trackbacks (1)
  1. If only you were in the same place as I am, I’d really buy you a beer and I’d be glad to shake my hands with you on this. Thank you for sharing.

  2. Hi, i have a similar, but yet quite different issue i’ve been trying to figure out for few days now.

    i have installed wordpress on my domain psykoid.com. i need to add a *.php file in my root directory, so that it is accessed via psykoid.com/*.php.

    i want to use my blog’s header and footer html so that *.php looks the same, but i can’t get the static html. is there such a way?

  3. hello there, I think this is what I am needing to use for my 3rd party support script (whmcs) (also uses smarty) but I am not clear on where the files should go after they have been created.

    I’ve never dealt with a shell script, so exactly how do I make it work? Also, where do the .html files listed in the shell script come into play?

    Thanks for any clarification you can give to this non-programmer!

  4. Excellent! However, I did it slightly differently.

    Created a new index.php file in a seperate folder did include the ../wp-blog-header.php then copied the content of wp-content/themes/MyTheme/page.php over into the index.php file

    Modified a little to suit my need and make this index.php file read content of other files that I wish to publish like abcd.c file based on the GET parameter (normal php coding).

    ex. http://www.mysite.com/othercontent/index.php?file=abcd.c

    Gotta becareful to allow index.php file to read only permitted content and not to allow index.php file to go into .. folders etc.

  5. You my friend, are an absoloute legend!
    I love you, and want your babies.

    Problem is im a guy and im assuming you are too, however…a beer your way is more than well deserved.

  6. Karl, thanks for your comments. Glad it helped you.

  7. I had a similar need WP+WHMCS but your steps did not work for me. Turns out any explicit JS that was in my WP page header or body, would make the WHMCS pages not display in IE. Do you know why is that?

    So then, since I could not just use the WP code as is, I went the manual route and created a blank page in WP, copied the HTML code and pasted the top part in WHMCS’s header.tpl and bottom part in footer.tpl. Tehn painstakingly had to insert the WHMCS markings that were originally there (saved for reference as header-original.tpl and footer-original.tpl).

    Took a while but it is working well now. Since it is all custom, I decided to leave out the WP main menu bar and links from the footer. This way I will not have to update (since it is a manual process) if it changes but is also good not to have those distracting links when the customer is on the WHMCS order pages…

    Makes sense? Let me know if you have any additional feedback on this…

    Thanks!


Leave a comment