Random Chunks


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.

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 http://www.shawnWilkerson.com
 * @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

Shawn is [now] working on our website. He [is] doing a great job working with our product manager and sales team in getting everything fined tune. He is not only great at what he does, he is also a good honest, hardworking person. If we have a problem we can call on Shawn and he will point us in the right direction to help us get our problem solved or be here in a very short time. He has always been available and his customer service is outstanding.

Lauren Schultz, Vice President
G.W. Schultz Tool INC.

Shawn is dependable, flexible, trustworthy, and professional. He takes pride in his work and his cost if very affordable. I highly recommend Mr. Wilkerson for any job...

Tony Barhoo, Pastor
Living Faith World Ministries
Open Source
Open Source