This works in Modx Revolution! (with tweaking needed)
Found this on Tim Spencer’s blog:
http://timspencerweb.co.uk/blog/?p=3#comment-867
I’m using it, with edits to work with
Modx Revolution 2.0.4 on a
custom external php page (photo gallery script) that is located in a subdirectory (/modx/core/compenents/myGallery/gallery-name/index.php) and it is displaying the contents of a modx chunk titled “header” correctly including the output of the Wayfinder menu.
But there’s a problem with the Wayfinder generated URLs.
The links in the menu are all written with the full directory path like this:
localhost/modx/core/compenents/myGallery/gallery-name/index.php?id=1
..should be:
localhost/modx/index.php?id=1
Any idea how to fix it?
Here is the code (found in ’/modx/core/compenents/myGallery/gallery-name/index.php’):
//modx object - test
require_once ‘../../../../../manager/config.core.php’;
require_once MODX_CORE_PATH.’config/’.MODX_CONFIG_KEY.’.inc.php’;
require_once MODX_CORE_PATH.’model/modx/modx.class.php’;
$modx = new modX();
$modx->initialize(’web’);
//tim’s code:
define(’MODX_SITE_BASE_URL’, ‘http://localhost/site/modx/’);
// The MODx document we are going to pretend to be
define(’MODX_DOCUMENT_IDENTIFIER’, 48);
// Some defines used by the modx API
define(’MODX_API_MODE’, true);
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;
//END Tim’s code
//DISPLAY HEADER CHUNK W/ NAVIGATION
$chunk = $modx->getChunk(’header’) ;
if($chunk){echo($chunk);}
//—————————
NOTE:
I found that when I add the
<base href="site_url"> to the page, like so:
/* gets the site_url setting */
$site_url = $modx->getOption('site_url');
//then add this in the pages <head>
<base href="<?php echo($site_url);?>" />
it fixes the links that Wayfinder creates, However the rest of the page breaks because the file is in a different directory.