We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I am doing a bit of work integrating an existing wordpress blog with a client's MODx site, and running into something I've not found any insight with online or in my forum searching. I have a small PHP file used in the blog navigation to mimic the main sites:

    define('MODX_CORE_PATH', '(path)/www/htdocs/core/');
    define('MODX_CONFIG_KEY', 'config');
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    $modx= new modX();
    $modx->initialize('mgr');
    $output = $modx->getChunk('blogNavigation');
    echo $output;
    


    It returns the chunk back into the wordpress template but it comes out as:

    <section id="nav">
    <nav>
      <ul class="navHolder">
        <li id="opensci"><a href="http://www.primerevenue.com/opensci/" ><span>OpenSCI</span></a>
    [[Wayfinder? &startId=`2` &level=`1`  &outerTpl=`wayfinderMenu` &rowTpl=`brokenMenu`]]
        </li>
    ...etc
    


    Is there a way to get the chunk then process the wayfinder calls or am I going about this the wrong way?

    Thanks in advance for any insight!

      Senior Designer at Arketi Group - http://www.arketi.com
      Twitter - @jalterixnar - https://twitter.com/jalterixnar

      “There is no dishonor in failing, only in the failure to try.” – Elder fire dragon Kaernyth
      • 37743
      • 49 Posts
      Hmm... not sure why Wayfinder isn't being processed... almost certainly something to do with the cacheing order. Have you tried calling wayfinder uncached? [[!Wayfinder...

      In a worst case scenario you could try something like this:

      Chunk:
      <section id="nav">
      <nav>
        <ul class="navHolder">
          <li id="opensci"><a href="<a href="http://www.primerevenue.com/opensci/"" target="_blank" rel="nofollow">http://www.primerevenue.com/opensci/"</a>
       ><span>OpenSCI</span></a>
      {WAYFINDER}
          </li>
      ...etc


      PHP:
      define('MODX_CORE_PATH', '(path)/www/htdocs/core/');
      define('MODX_CONFIG_KEY', 'config');
      require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
      $modx= new modX();
      $modx->initialize('mgr');
      $chunk = $modx->getChunk('blogNavigation');
      $wayfinder = $modx->runSnippet('Wayfinder',array( /** params here **/));
      $output = str_replace('{WAYFINDER}',$wayfinder,$chunk);
      echo $output;
      

        • 33968
        • 863 Posts
        Snippets are not parsed when using the getChunk method. It just returns the contents of the chunk, as it is doing in your case.

        You could try using getParser to parse the contents of the chunk externally:

        define('MODX_CORE_PATH', '(path)/www/htdocs/core/');
        define('MODX_CONFIG_KEY', 'config');
        require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
        $modx= new modX();
        $modx->initialize('mgr');
        $chunk = $modx->getChunk('blogNavigation');
        
        $maxIterations = (integer) $modx->getOption('parser_max_iterations', null, 10);
        $modx->getParser()->processElementTags('', $chunk, false, false, '[[', ']]', array(), $maxIterations);
        $modx->getParser()->processElementTags('', $chunk, true, true, '[[', ']]', array(), $maxIterations);
        echo $chunk;
        
        [ed. note: okyanet last edited this post 12 years ago.]
        • In addition, Wayfinder works based on the current Resource being rendered by the MODX request handling. IOW, it's not really intended to work externally like this. You would need to set various MODX variables, like $modx->resourceIdentifier, $modx->resource, etc., and I'm not sure what those would be if you are not even on a MODX resource.

          getChunk does process it's own content; there is another reason it is not processing this, but it's not clear why.
          • It might work better to put your blogNavigation chunk on a resource with an otherwise empty template, then use curl to request that resource from within Wordpress. That way Wayfinder will process as it would normally on that resource.

            I found this code in a forum post somewhere, and it works for me for this purpose. Place this in your functions.php:
            function get_modx_content($url) {
                $base_url = 'url-of-my-modx-domain';
             
                $full_url = $base_url.$url;
                $ch = curl_init();
                curl_setopt ($ch, CURLOPT_URL, $full_url);
                curl_setopt ($ch, CURLOPT_HEADER, 0);       
                ob_start();
                curl_exec ($ch);
                curl_close ($ch);
                $string = ob_get_contents();
                ob_end_clean();  
                echo $string; 
            }
            

            Then just call that function in your Wordpress template where you want the nav to appear, like this:
            <?php get_modx_content('my-wordpress-nav-resource.html'); ?>