We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27708 MODX Staff
    • 2,502 Posts
    So I am wondering what people about how they would integrate a Wordpress site (a legacy client site that has hundreds if not thousands of WP posts) w/ a MODx Evo 1.0.3 site. I want to use the same top part of the page and bottom

    In the MODx site the page top down to the content and site bottom from the bottom of the content to the footer are in chunks. I was thinking I’d like to have WP grab the top and bottom bits from MODx and use where you would use the get_header() and get_footer() functions. There are several considerations. The MODx header chunk has the document [*pagetitle*] in it and therefore I couldn’t just call it using the MODxapi.php from WP. I had thought about creating a document resource in Evo with a pagetitle of [+pagetitle+] and then using an external call to the page via the api to first set the [+pagetitle+] placeholder then call the page (the page itself may need another snippet to insert the placeholder I am not sure.)

    This is all sounding way to complicated when I could just copy the top chunk code and replace the MODx placeholders with the WP functions and then hardcode the main nav and section nav it just means I’d need to update them when the site changes.

    Tell me what you think.
      Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
      • 20413
      • 2,877 Posts
        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
        • 4310
        • 2,310 Posts
        Maybe this thread is a possible method.
          • 18913
          • 654 Posts
          Keeping in mind that this is probably not very efficient (see OpenGeek’s comment here http://modxcms.com/forums/index.php/topic,47741.0/topicseen.html ), this is the code used in this discussion
          http://modxcms.com/forums/index.php/topic,44791.10/

          NOTE : the responsiveness of the site referenced in that thread feels sluggish, so if I were going to try to do this again, I’d probably consider some of the other approaches mentioned. That said, this might give you some ideas, as I got MODx content showing up in WP and WP plugins running in MODx.

          In a test install of Evo 1.0.2 with WP set up in a subfolder called "blog" I had
          - this as the header in WP :
          <?php require_once('modx_bridge.php'); ?>
          <?php
          /**
           * @package WordPress
           * @subpackage Classic_Theme
           */
          ?>
          <?php if (MODX_WP == 'WP') { echo modx_chunk('minimal_header1'); } ?>
          	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
          	<?php wp_get_archives('type=monthly&format=link'); ?>
          	<?php //comments_popup_script(); // off by default ?>
          	<?php wp_head(); ?>
          <?php if (MODX_WP == 'WP') { echo modx_chunk('minimal_header2'); } ?>
          <div id="rap">
          <!--<h1 id="header"><a href="<?php bloginfo('url'); ?>/"><?php bloginfo('name'); ?></a></h1>-->
          
          <div id="content">
          <?php if (MODX_WP == 'WP') { echo modx_chunk('test_modx_chunk'); } ?>
          <!-- end header -->
          


          - this as the footer in WP :
          <?php require_once('modx_bridge.php'); ?>
          <?php
          /**
           * @package WordPress
           * @subpackage Classic_Theme
           */
          ?>
          
          
          </div>
          <?php get_sidebar(); ?>
          
          <p class="credit"><!--<?php echo get_num_queries(); ?> queries. <?php timer_stop(1); ?> seconds. --> <cite><?php echo sprintf(__("Powered by <a href='http://wordpress.org/' title='%s'><strong>WordPress</strong></a>"), __("Powered by WordPress, state-of-the-art semantic personal publishing platform.")); ?></cite></p>
          <?php if (MODX_WP == 'WP') { echo modx_chunk('minimal_footer'); } ?>
          </div>
          <?php wp_footer(); ?>
          <?php if (MODX_WP == 'WP') { echo modx_chunk('minimal_end'); } ?>
          
          


          - this in "modx_bridge.php", from Tim Spencer’s work here
          http://timspencerweb.co.uk/blog/?p=3%29" target="_blank" rel="nofollow">http://timspencerweb.co.uk/blog/?p=3%20%28http://timspencerweb.co.uk/blog/?p=3%29
          <?php
          if (!defined('MODX_WP'))
          {
          	// MODx integration
          	// -------------------------------------------------------------------------------
          	define('MODX_WP','WP');//MattC
          	// ----- CONFIG -----
          	define('MODX_SITE_BASE_URL', 'http://www.website.url/');  //needs a trailing slash
          	define('MODX_MANAGER_PATH', '/home1/path_to_modx_manager');  //no trailing slash
          	define('MODX_DOCUMENT_IDENTIFIER', 2); // 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
          	//$GLOBALS['database_type']='mysql';
          	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))))));
          	} 
          	function modx_snippet($snippet_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->runSnippet($snippet_name))))));
          	} 
          }
          ?> 


          - in MODx, "minimal_header1" looks like this :
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          
          <head>
          	<title>[(site_name)] | [*pagetitle*]</title>
          	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
          	<link rel="stylesheet" href="[(base_url)]assets/templates/default/site.css" type="text/css" media="screen" />
                  <base href="[(site_url)]" />
          
          <!--[if lte IE 6]>
          
            <style type="text/css" media="screen, tv, projection">
                  body { behavior: url(assets/js/csshover3.htc); } /* hover:anything support */
                  #content { margin-left: 22px; } /* to avoid the BMH */
                  a,  a:link { border-bottom-style: solid } /* becuase IE just doesn't dot */
            </style>
          
            <script type="text/javascript" src="assets/js/sleight.js"></script>
          
          <![endif]-->
          
          


          - in MODx, "minimal_header2" looks like this
          </head>
          <body>
          
          <div id="page">
          
          
          <div id="header">
          	<div id="search"><a name="search"></a>
          		<!-- if you wanted a site search, this would be a good place -->
                          Blog name : [!wp_bloginfo? &id=`name` !]
          	</div>
          	<h1><a href="[~[(site_start)]~]" title="[(site_name)]">[(site_name)]</a></h1>
          </div>


          ... etc, etc. for the footer.

          The MODX_WP flag was my attempt at trying to streamline some logic. In hindsight, the logic test just unnecessarily adds to overhead, I imagine.

          And for some reason that I can’t recall, my wp_bloginfo snippet isn’t working correctly. I was probably messing around with it, started on another project and never tidied it up.

          Matt
            • 27708 MODX Staff
            • 2,502 Posts
            So, I finally decided there is no advantage to doing anything but maintaining 2 versions of the top and bottom. Mainly as the effort required is way higher than the copying and pasting of a few lines of code vs any need to do this again which is limited.

            I literally spent 3 hours thinking about this and posting about it and looking at approaches. I wasted most of that time.

            Lesson is if you don’t anticipate need to repeat an implementation and the effort to do something even twice is less, do that.

              Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
              • 27708 MODX Staff
              • 2,502 Posts
              So I am back at it because there were too many moving parts like a PieX bit in the footer, a ditto call in the footer and a wayfinder call and some conditional side bar wonderfulness. I may have to pull out some other bits but I have most methods but the only method I got working in any way (not perfectly) was netProphet’s here: http://modxcms.com/forums/index.php/topic,34880.msg211619.html#msg211619

              Just thought I’d update the progress. I had no measure of success with the modxapi.php as it threw a "Can’t connect to DBAPI" error which is what happens when the parser fails to connect to it.

              I am going to tap away at it. Any more thoughts let me know.
                Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                • 7253
                • 97 Posts
                Attached is a wordpress plugin that I wrote that provides this functionality.

                It provides the following methods that you can call from within wordpress templates:


                • modxGetChunk($chunk_name)
                • modxRunSnippet($snippet_name, $param_string)

                You can call them from your wordpress template like this:

                <?php modxGetChunk('page_footer'); ?>
                


                It’s slightly rough and ready but it seems to work ok. I use it on my site (visit that link, then click on ’blog’ - pretty seamless, no wink) and some client sites use it too. I generally put the wordpress install in a /blog folder inside the modx site root, so you get:

                [tt]
                /
                /assets
                /manager
                /blog
                [/tt]

                Then you can just put a weblink into you MODx site that points to /blog, so you can link to it, and clicking it will just take you over to the wordpress blog; this is how the Codeistry site’s /blog bit works. The plugin sort of assumes this folder layout, so if yours is different, you’ll need to edit line 19 of modx_init.php, so that it can find the modx config file. Just edit the
                $manager_path = '../manager';
                bit.

                The modx_config.php file is loaded after the actual modx config.inc.php file and allows you to selectively override MODx config settings for the plugin.

                Just extract the attached .zip file into your wordpress /wp-content/plugins/ folder, login to your wordpress manager and enable the plugin, then alter your wordpress theme to use the functions in the appropriate places.

                This is simple and it seems to work well - let me know if you have any problems. I realise that it could probably do with a tidy up, not to mention a Wordpress Manager control panel, but hey, it works and I’m busy smiley

                Patches welcome!
                  • 27708 MODX Staff
                  • 2,502 Posts
                  Duncan! Where were you last week dude! I beat on this for hours and still only sorta got it working. I am trying this out now!!! I’ll let you know how it goes. If so you need to release this to the world.
                    Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                    • 4310
                    • 2,310 Posts
                    Nice job Duncan smiley
                      • 27708 MODX Staff
                      • 2,502 Posts
                      So Duncan,

                      It didn’t initially work 100% but I did manage to make some changes to get it working perfectly.

                      First, the calls to MODx resources using the [~id~] i.e., [~5~] was not getting turned into an URL so made the following change to modx_integration.php at line 54:

                      Changed this:
                       $chunk_content = $modx->parseDocumentSource($chunk_content);
                      

                      To this:
                      $chunk_content = $modx->rewriteUrls($modx->parseDocumentSource($chunk_content));
                      


                      Second, no matter what I set the base_url to in the modx_config.php it wouldn’t set the site_url correctly and all my paths and all instances of my use of [(site_url)] showed the blog subdir as the root i.e., http://www.example.com/dev/blog/ so my css and base_url and the / link which pointed to home all had the wrong address.

                      Therefore I added the following the bottom of the modx_init.php file:

                      <?php //for highlighting
                      // Selectively override global modx config from plugin local version
                      $modx->config['base_url'] = $modx_config['base_url'];
                      $modx->config['site_url'] = $modx_config['base_url']; //added this line and all was good.
                      


                      Finally I wanted to make wayfinder work to show the correct active nav in the heade so I also placed the following in the modx_init.php file just after the following:
                      <?php //for highlighting
                      // Start-up MODx
                      $modx->db->connect();
                      $modx->getSettings();
                      


                      <?php //for highlighting
                      $modx->documentIdentifier = '23'; //ID of the Weblink Resource to the Blog shows its parent as active in the nav.
                      $modx->documentObject['pagetitle'] = 'My WP Blog'; //sets the pagetitle for the titlebar and the content header of the page
                      


                      Now it is working absolutely perfectly and I am still super stoked about this. Thanks Duncan!
                        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub