Integrating Wordpress and MODx
So at last I join the ever-growing world of blogging. Being a developer of modx sites I considered whether to use the various snippets available for building a blog in MODx, having done this before. However its always good to experiment so instead I opted to link a WordPress blog with my own existing MODx site.
I have recently linked SMF to MODx for the Mountain Rescue England and Wales website, and so adapted the code I used there.
I use the code (below) in an include file which is then included in each of the theme files that represents a WordPress page. Alternatively and to be on the safe side, use ‘require_once’ in every php file that needs data from MODx.
You will see from looking at the code that I have set things so that as far as the MODx API is concerned we are on the document/resource with id 31, which is the MODx weblink to this blog. As such, menu snippets such as Wayfinder will correctly identify docid 31 as being the page we are on. In addition I run the getDocumentObject method so that the template variables used by this document/resource are available.
Lastly I provide a function to parse a MODx chunk as this is what I mainly want it to do (for the header and the navigation in the top right corner).
<?php
// MODx integration
// -------------------------------------------------------------------------------
// ----- CONFIG -----
define('MODX_SITE_BASE_URL', '/');
define('MODX_MANAGER_PATH', '/home/timspe/public_html/TimSpencerWeb_modx/manager');
define('MODX_DOCUMENT_IDENTIFIER', 31); // The MODx document we are going to pretend to be
// ------------------
// Some defines used by the modx API
define('MODX_API_MODE', true); // Tells MODx index.php to not run $modx->executeParser
define('MODX_SITE_URL', (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) == 'off' ? 'http://' : 'https://').$_SERVER['SERVER_NAME'].MODX_SITE_BASE_URL);
// Run the MODx config and get site settings
require(MODX_MANAGER_PATH.'/../index.php');
$modx->getSettings();
// Set the docid (most sensibly to the MODx weblink to here) and get the MODx document object
$modx->documentObject = $modx->getDocumentObject('id', MODX_DOCUMENT_IDENTIFIER);
$modx->documentIdentifier = MODX_DOCUMENT_IDENTIFIER;
// Set the base URL for any MODx snippets
$modx->config['base_url'] = MODX_SITE_BASE_URL;
// Get AND parse a modx chunk. Cannot cope with nested snippets within the chunk.
function modx_chunk($chunk_name)
{
global $modx;
return $modx->rewriteURLs(str_replace('[~', MODX_SITE_URL.'[~', // Ensure that the link goes to the MODx site and not e.g. /blog/pagename
$modx->parseDocumentSource(str_replace('[!', '[[', str_replace('!]', ']]', $modx->getChunk($chunk_name))))));
}
?>
Hi Tim,
Thank you very much for posting this blog and in particular the MODx-WP integration info. I managed to get it loaded and have it display a Wayfinder menu, basically following the instructions you laid out.
In my case, I have been trying to blend MODx and WP in a local install. So my setup was on XAMPP (Windows Vista), with WP 2.9.1 and MODx Evo 1.0.2. Wordpress was installed under the main directory for my testsite. For example, if the testsite path was localhost/testsite (under which would be the MODx assets, manager, etc. folders) then wordpress lives at localhost/testsite/wordpress.
I put your code in a file called modx_bridge.php, which I found I needed to have in the directory for the theme I was using. This theme was called “swansong” and the process for cutting it up into a MODx theme can be found at this site (not mine ) : http://codingpad.maryspad.com/2009/03/31/building-a-website-with-modx-for-newbies-part-3-working-with-templates/
While that site uses MODx 0.9.6.3 as the platform, the process for taking an existing theme and getting it to work with MODx is instructive. It was also handy in then getting that theme to work under Wordpress, as the slicing-and-dicing is very similar.
To correctly work, I needed to edit your code for my install. So my config lines look like this :
define(’MODX_SITE_BASE_URL’, ‘http://localhost/testsite/’);
define(’MODX_MANAGER_PATH’, ‘c:/xampp/htdocs/testsite/manager’);
I also needed to create a new document in the MODx install, which I set up as a weblink, named “WP-blog.” The resource id for this document was also put into your code.
So at this point I have a theme in Wordpress that is working and your code - customized for my setup - sitting in its own file in the theme directory.
In order to change the theme to get it to run your code, I put the following line at the beginning of each file that made up the theme. (This was probably overkill and over time I’m sure I’ll find a few that didn’t need to be touched.)
<?php require_once(’modx_bridge.php’);?>
Finally, my menu call was at the end of my header.php file. I replaced it with this
<?php echo modx_chunk(’swansong_nav_horiz’); ?>
which is a call to the chunk I have in MODx which contains a Wayfinder call.
Early efforts at getting this to work failed because I had a bit of leftover PHP from an earlier editting effort that was causing a blank screen, as well as a lack of the “echo” part of the PHP call.
At the moment the menu is working, but I think I have other issues with my template that are preventing individual stories from coming up. However, I was able to accomplish the same thing by taking one of the Wordpress themes available online (specifically, Grunge Music 1.3) and go through the same routine. This time, however, I edited sidebar.php to include the MODx menu, which accessed by putting this code after the last <li> block in that file:
<li><h2>MODX menu</h2>
<ul>
<?php echo modx_chunk(’swansong_nav_horiz’); ?>
</ul>
</li>
So the basic integration/skinning of the two seems to be pretty easy to accomplish. What I am now wondering is : Is there a mapping between MODx and WP template tags that would help the skinning process? As well, does WP have a similar parser/engine that can be fired up from within MODx to allow MODx access to some of WP’s routines?
Any thoughts would be appreciated. And I hope the above is of some help.
Matt
Thanks for the info Matt. Glad to hear the code is working for others as well as myself; this sort of feedback is useful (and reassuring!)
Matt has also let me know about some info he has posted on the MODx forums on doing the reverse i.e. running Wordpress functions from MODx.
See here http://modxcms.com/forums/index.php?topic=44791.msg265706#msg265706. Its worth taking a look.
– Tim.
I’ve just posted a comment on the MODx forum. Basically, it seems that
- the “wp_bridge” snippet that allows WP functions to be called in MODx doesn’t need to be called at the start of a MODx template, if there is the include statement (noted in the forum) in the “index.php” file. The forum thread is here http://modxcms.com/forums/index.php/topic,44791.0.html
- And on the WP side, it looks like the “modx_bridge” code can be included in the functions.php file. This then means that the “require_once” statement doesn’t need to be included in the various templates.
…which simplifies things a bit. I might find out that I’ve overlooked something, but if anyone is trying this out, it might be worth just testing those two changes first.
MattC
Do you know if this would work in Modx Revolution? I can code everything into a WP template except the nice Wayfinder menu!
This code will almost certainly not work ‘as is’ in MODx Revolution due to the quite different API - alot of Evo’s API functions either do not exist at all or - in at least one case - return different results.
It may be worth looking for a generic MODx Revolution API to use with your WP templates. Whether one exists yet in stable form I don’t know.
– Tim.