Displaying MODX Context Settings

A user on the MODX Forums asked for a way to show a Context Setting from a Context other than the current one. In this case the setting was the site_start setting (I think the user wanted to use it in a link), but the code below will show any Context setting.

As you probably know, you can show a Context Setting for the current Context with a simple setting tag:

[[++site_start]]

The tag above will be replaced by the site_start Context Setting if one is set. If not, it will be replaced by the System Setting (unless there is a site_start User Setting and the user is logged in). User Settings have the top priority, then Context Settings, then System Settings.

A setting tag will always show the Context Setting or System Setting for the current Context unless it’s overridden by a User Setting. If you want to refer to another context, it gets a little trickier. Here’s a snippet that will get a setting from the named context, and will report any errors in the process.

Here is the snippet tag:

[[getContextSetting? &context=`someContext` &setting=`site_start`]]

Here is the code to paste into the getContextSetting snippet:

<?php
/* getContextSetting snippet */

$ctx = $modx->getOption('context', $scriptProperties, null);
$setting = $modx->getOption('setting', $scriptProperties, null);

if ($ctx == null) {
    return 'No Context set';
} elseif ($setting === null) {
    return 'No Setting set';
} else {
    $csObj = $modx->getObject('modContextSetting',
        array(
            'context_key' => $ctx,
            'key' => $setting
        )
    );
}

if ($csObj) {
    return $csObj->get('value');
} else {
    return 'Context Setting not found';
}

If you’ve put the correct Context and Setting-name values in the tag, the tag will be replaced by the value of the named key from the specified Context.

Note that unlike using a setting tag, this code gets the Context Setting directly, so it will never show a System Setting or User Setting. That means it can also be used to get a Context Setting in the current context and the value won’t be overridden by a User Setting of the same name.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


4 × 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>