Using Wordpress header and footers externally
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.
Heres 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/YOUR_THEME/header.php"; ?>
and footer-wrapper.php looks like this
<?php require('./wp-blog-header.php'); include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/YOUR_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”}
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!
Related posts:

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.
Leave your response!