Creating a shortcode from the results of an array_rand function Creating a shortcode from the results of an array_rand function wordpress wordpress

Creating a shortcode from the results of an array_rand function


Creating a shortcode from the results of an array_rand function

I suggest you look at the problem from a different angle. Rather than creating a short code from the results of an array_rand call, it seems to me that you want to define a single short code which will display a random banner.

If you want a short code that returns a random banner, something like this might work:

function random_banner() {    $banner_indexes = ['01', '02', '03', '04', '05'];    $index = $banner_indexes[array_rand($banner_indexes)];    return sprintf('<a href="/path/" class="exampleclass" id="example-code-%s"><img class="example%simgclass" src="path/example%s.jpg" alt="Example %s"/></a>', $index, $index, $index, $index);}add_shortcode('random-banner', 'random_banner');


This is how I would do it

function examplecode01(){    return __FUNCTION__;}function examplecode02(){    return __FUNCTION__;}function examplecode03(){    return __FUNCTION__;}function examplecode04(){    return __FUNCTION__;}function examplecode05(){    return __FUNCTION__;}function examplecode() {     $functions = array('examplecode01', 'examplecode02', 'examplecode03', 'examplecode04', 'examplecode05');     shuffle($functions); //randomize array    $function = array_shift($functions); //get first array element    return function_exists($function) ? $function() : '';} //add_shortcode('example-code-random', 'examplecode');echo examplecode();

Sandbox

The __FUNCTION__ "magic" constant is the name of the current function, you can replace this with your actual code, it just makes seeing what function was called a bit easier.

To be honest instead of doing this examplecode01 and examplecode02 (iterative functions) I would just make one short code (to rule them all) like:

[examplecode]  //random[examplecode banner="1"] //examplecode01

And so on, it will be easy to expand on later. Something like this:

function examplecode($attr) {     $banners = array('example01.jpg', 'example02.jpg', 'example03.jpg', 'example04.jpg', 'example05.jpg');     $atts = shortcode_atts(array(        'banner' => false    ), $atts );    if($atts['banner'] && isset($banners[$atts['banner']-1])){        //make sure to check if the index exists        //you could handle this differently, such as not show a banner, show an error message etc...        $index =  $atts['banner']-1;  //0 based vs 1 based    }else{        $index = rand(0, (count($banners)-1));    }    //pad with a leading 0 if less then 2    $i = str_pad($index+1, 2, '0', STR_PAD_LEFT);           return '<a href="/path/" class="exampleclass" id="example-code-'.$i.'"><img class="example'.$i.'imgclass" src="path/'.$banners[$index].'" alt="Example '.$i.'"/></a>';} add_shortcode('examplecode', 'examplecode');

See if you do function{n} and you want to add a banner, you have to make a whole new function and shortcode. However, if you use the shortcode attributes then you only need to add the image to the array. No "banner" attribute it's random, otherwise it's the banner image number from the array. There is a bit of jiggling to keep your banners 1 based because arrays are 0 based. You can simplify it a lot by starting at 0 instead. But whatever...

The last thing is whenever I write shortcodes I wrap them in a function exists, you don't have to do that. It's just something I like to do, just to be safe.

if(!function_exists('examplecode')){    function examplecode($attr) { ... }}

Basically it prevents it from defining the function if it's already been defined.