We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12379
    • 460 Posts
    Integrating MODx theme into vBulletin which involves the slightly tedious task of running modx’s api externally in a header and footer php file to form the interface shell around the forum. Problem is the functions I’m reusing from the header file (to get and parse MODx chunks) throws an error when inserted in the footer file, so I get the error:

    Fatal error: Call to undefined method MODxAPI::modx_chunk() in /home/xxx/public_html/vb_footer.php on line 89 ... for not having the function in the footer file, and:

    Fatal error: Cannot redeclare modx_chunk() (previously declared in /home/xxx/public_html/vb_header.php:20) ... when I do include it.

    Have tried including in a seperate config file but the function renders on a blank screen as bits of plain text.

    Header:
    <?php
    
    // ----- CONFIG -----
    $path = dirname(__FILE__).'/'; // '/path/to/api/file'
    define('MODX_SITE_BASE_URL', '/');
    define('MODX_SITE_URL', (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) == 'off' ? 'http://' : 'https://').$_SERVER['SERVER_NAME'].MODX_SITE_BASE_URL);
    $modx_baseurl = MODX_SITE_BASE_URL;
    
    include_once($path.'modxapi.php');
    
    $modx = new MODxAPI();
    // connect to db and load settings
    
    $modx->connect();
    $modx->getSettings();
    
    // Set the base URL for any MODx snippets
    $modx->config['base_url'] = MODX_SITE_BASE_URL; 
    
    //This is the function I need to reuse:
    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. /forum/pagename
          $modx->parseDocumentSource(str_replace('[!', '[[', str_replace('!]', ']]', $modx->getChunk($chunk_name))))));
       } 
    
    $html = modx_chunk('header-chunk-here');
    echo $html;
    
    //etc


    Footer:
    <?php
    
    //Redeclared function should go here?
    
    $html = modx_chunk('footer-chunk-here');
    echo $html;
    
    //etc 


    Thanks in advance...
      Mostly harmless.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      <?php
      if ( !function_exists('modx_chunk')) {
          function modx_chunk($chunk_name) {
              ...
          }
      } ?>

      http://php.net/manual/en/function.function-exists.php
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 12379
        • 460 Posts
        Thanks but same error "Fatal error: Call to undefined method..."
          Mostly harmless.
          • 12379
          • 460 Posts
          My bad! Was using:

          $html = $modx->modx_chunk(’footer-chunk-here’);

          instead of:

          $html = modx_chunk(’footer-chunk-here’);
            Mostly harmless.