1

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)

1 Answer 1

2

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]' );
Sign up to request clarification or add additional context in comments.

3 Comments

that was perfect, It only displays this warning, what could be the reason? Warning: Undefined array key in /public_html/wp-content/themes/test-child/index.php on line 20 ( if ( ! is_array( $GLOBALS['my_shortcode_used'][ $post_id ] ) ) {)
if ( ! isset( $GLOBALS['my_shortcode_used'][ $post_id ] ) || ! is_array( $GLOBALS['my_shortcode_used'][ $post_id ] ) ) Please replace the above condition with this condition.
It was wonderful, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.