Multiplay Labs

tech hits and tips from Multiplay

Integrating Legacy PHP applications with WordPress

with 2 comments

Recently we wanted to integrate some existing legacy code into a new wordpress site we were developing. Obviously, we didn’t want to have to reimplement all the code for the wordpress theme separately in a standalone way just to make the two codebases visually consistent, so we looked into how to integrate the two, allowing us to make use of the wordpress theme within the new code.

This turned out to be surprisingly simple, albeit with a few caveats.

First of all, we made sure the legacy codebase had access to the wordpress installation. Next, in our main include in the legacy code (a setup/config script known to always be included), we added the following code:

require( "{$_SERVER['DOCUMENT_ROOT']}/wordpress/wp-load.php" );

This causes all pages in the legacy app to load the wordpress framework. Contrary to instructions on the WordPress Codex (which direct you to require wp-blog-header.php) this serves the page correctly, rather than as a wordpress 404 page. Obviously, you should adjust the require path to match your own environment.

Once this was done, in the legacy applications header and footer includes, we could use wordpress functions such as get_header() and get_footer() to include the parts of the theme we needed.

However, there are some caveats to this approach when integrating with legacy codebases. We found that the database connection of wordpress was overriding the database connection established by our legacy application. Since they had different permissions, the legacy application failed to perform any of it’s queries. A quick hack of resetting the connection back to the app after rendering the header, then restoring the wordpress connection prior to rendering the future fixes this. Obviously the correct solution is to update the legacy app to use a specific database connection object rather than the default of the last connection established, but this isn’t always feasible for some projects due to the time/resources involved.

Written by Andrew Montgomery-Hurrell

July 11th, 2013 at 8:19 am

Posted in PHP,Wordpress

Tagged with ,

2 Responses to 'Integrating Legacy PHP applications with WordPress'

Subscribe to comments with RSS or TrackBack to 'Integrating Legacy PHP applications with WordPress'.

  1. Ah thank you Andrew Montgomery-Hurrell !!! I knew there was a simple way to include wordpress themes into standalone php apps! I was real depressed searching for three hours and I found this your page and drank it in – now I feel good about life again!!! (Programmers are crazy aren’t we?)

    You need anything (for free) just let me know!
    Jody
    http://www.tucsonwebbuilders.com

    jbrabec

    24 Sep 15 at 07:20

  2. No worries, glad it helped. I remember what a pain it was to figure it out, so I’m glad I spared someone that torment!

Leave a Reply

You must be logged in to post a comment.