phpwordpressshortcodewordpress-shortcode

Problem in displaying the WordPress shortcode function randomly


I have a shortcode function similar to the following code:

function my_shortcode() {
    $arr = array( 
        '1' => '<a href="https://google.com"> <img src="img_1.jpg" /> </a>', 
        '2' => '<a href="https://yahoo.com"> <img src="img_2.jpg" /></a>', 
        '3' => '<a href="https://apple.com"> <img src="img_3.jpg" /></a>',
        '4' => '<a href="https://zdf.com"> <img src="img_4.jpg" /></a>',
        '5' => '<a href="https://air.com"> <img src="img_5.jpg" /></a>',
        '6' => '<a href="https://bing.com"> <img src="img_6.jpg" /></a>' 
    );
    $key = array_rand($arr);
    return print_r($arr[$key]);
}
add_shortcode('joke', 'my_shortcode');

And I plan to use this function in several different places of different posts and each of these shortcodes will randomly execute one of the commands of the function.

This shortcode function is supposed to display a linked image. I want, for example, if I use this shortcode 3 times in one post [joke], I don't want it to display a duplicate value! (I want to display a different value every three times I use it in a post)


Solution

  • By using $GLOBALS we can achieve this. $GLOBALS['my_shortcode_used'] is a custom global variable that stores information specific to the [joke] shortcode per-post basis, preventing duplicate values from being displayed within the same post.

    function my_shortcode() {
        $values = [
            '1' => '<a href="https://google.com"> <img src="img_1.jpg" /> </a>',
            '2' => '<a href="https://yahoo.com"> <img src="img_2.jpg" /></a>',
            '3' => '<a href="https://apple.com"> <img src="img_3.jpg" /></a>',
            '4' => '<a href="https://zdf.com"> <img src="img_4.jpg" /></a>',
            '5' => '<a href="https://air.com"> <img src="img_5.jpg" /></a>',
            '6' => '<a href="https://bing.com"> <img src="img_6.jpg" /></a>',
        ];
    
        if ( ! isset( $GLOBALS['my_shortcode_used'] ) ) {
            $GLOBALS['my_shortcode_used'] = [];
        }
    
        $post_id = get_the_ID();
    
        if ( ! isset( $GLOBALS['my_shortcode_used'][ $post_id ] ) || ! is_array( $GLOBALS['my_shortcode_used'][ $post_id ] ) ) {
            $GLOBALS['my_shortcode_used'][ $post_id ] = [];
        }
    
        $unused_values = array_diff( $values, $GLOBALS['my_shortcode_used'][ $post_id ] );
    
        if ( empty( $unused_values ) ) {
            $GLOBALS['my_shortcode_used'][ $post_id ] = [];
            $unused_values                            = $values;
        }
    
        $key = array_rand( $unused_values );
    
        $GLOBALS['my_shortcode_used'][ $post_id ][] = $unused_values[ $key ];
    
        return $unused_values[ $key ];
    }
    add_shortcode( 'joke', 'my_shortcode' );
    

    Now use shortcode three times in you single.php

        echo do_shortcode( '[joke]' );
        echo do_shortcode( '[joke]' );
        echo do_shortcode( '[joke]' );