The functions.php file of my WordPress site currently has in place blocks of code like this:
function examplecode01() {
$i = '<a href="/path/" class="exampleclass" id="example-code-01"><img class="example01imgclass" src="path/example01.jpg" alt="Example 01"/></a>';
return $i;
}
add_shortcode('example-code-01', 'examplecode01');
There are 5 or more of these, each with their respective variations of "example code ##" and such as described above. The shortcode line allows for an editor to specify a shortcode and pull in a specific banner image into a Blog post (using the first parameter of the add_shortcode), as follow:
[example-code-01]
What I wanted to do instead was to randomize it, in such a way that an editor can use the same shortcode anywhere and it will be a random banner image from those available.
In working toward this, I slightly modified the above code block as follows:
function examplecode01() {
echo '<a href="/path/" class="exampleclass" id="example-code-01"><img class="example01imgclass" src="path/example01.jpg" alt="Example 01"/></a>;
}
(I removed the shortcode line because it would cause problems in the output of the next section -- bear with me).
Okay, so there's several of the modified code blocks, each with their own image. At the end of them, I then have a function, as follows:
$functions = array('examplecode01', 'examplecode02', 'examplecode03', 'examplecode04', 'examplecode05');
$functions[array_rand($functions)]();
When I throw this into an otherwise-blank PHP file (for testing) and run it online, it outputs a random banner image from the ones I've listed. Hooray! Success... sort of.
See, what I need now is a way for the editors to call up that random result by way of a shortcode. I'm not 100% sure on how to make this happen, though. The original shortcode basically was ["id used in the code block", "function name"]
I thought about setting the result to a variable and then calling that variable, but I'm still not sure how it would "convert" (so to speak) to a shortcode...
Can anyone help me with this final part of the puzzle? Thank you in advance!