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.
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!
Related posts:
- CubeCart and Wordpress Integration
Following my 100% CubeCart orientated posts, here's another late night hack. It integrates a Wordpress... - Custom Page Templates Not Showing in Wordpress
I've recently starting getting an annoying error in Wordpress, I'm not sure if its my... - mod_geoip Revisted including stopping spam in phpBB 2
I've recently moved a client's forum onto a new cPanel server, previously it was on... - Retro Fitting Record Pagination with ADOdb
For my first project of the New Year I had to retro-fit pagination controls to... - Removing Wordpress plugin references
I'd like remove all references Wordpress plugins put into my HTML code. Am I selfish?...
September 26th, 2008 - 15:43
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.January 20th, 2009 - 16:50
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?
February 23rd, 2009 - 04:31
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!
August 16th, 2009 - 13:46
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.
December 14th, 2009 - 00:23
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.
December 14th, 2009 - 08:52
Karl, thanks for your comments. Glad it helped you.