PHP Source Code


 * File randomChunk.php (requires MODx Revolution 2.1+)
 * Created on: 10/13/11 at 7:02 PM
 * Updated on: 05/06/2015 at 14:43.
 * Project shawn_wilkerson
 * @version 2.0
 * @category
 * @author W. Shawn Wilkerson
 * @link
 * @copyright Copyright (c) 2011, Sanity LLC. All rights reserved.
 * @license GPL 2
 * Version 2 Limited the scope of the incoming variables to allow the parser to better randomize multiple calls in the same page.
$out = false;
$prefix = $modx->getOption('chunkPrefix', $scriptProperties, 'random');
$chunks = $modx->getOption('totalChunks', $scriptProperties, intval(0));

if ($chunks > 0) {
    $chunk = $modx->getObject('modChunk', array(
        'name' => $prefix . rand(1, ($chunks)),
if (is_object($chunk)) {
    $out = $chunk->process();
return $out;

Example Snippet Calls

[[!randomChunk?chunkPrefix=`banner`&totalChunks=`4`]] -- user selectable prefix
[[!randomChunk?&totalChunks=`29`]] -- defaults to random as the prefix

Example Output

World Wide Stationery Manufacturing located in Hong Kong and Mainland China; ... were needing to brand our Company name and products... through a web site. Shawn [continues] to find new ways to present our products and Brand our name, as we gain recognition within the Bindery Industry [as the world's largest supplier of ring metals].

Russ Truelove, Representative
World Wide Stationery Manufacturing Group

You’re the best communicator, and that’s what brought about my satisfaction and a website that does all I need it to do.

Thomas Noton, President
Tidy Seed® Products


Like most randomization code, this relies on a higher number of items to choose from to produce a more noticeable random effect. With fewer modChunks to choose from, it becomes no different than the predictability of flipping coins for heads and tails.

I also created a sub Category to minimize clutter in the Element Tree. This can also become the foundation of eventually implementing a random MODX Revolution Snippet centered on categories, where only the category name is required in the Snippet call.

Also notice, I force the direct process of the modChunk before its being sent to the page.

MODX 2.3+

MODX 2.1 - 2.2