• Resolved senator94

    (@senator94)


    I have created a PHP function for random text. Ideally, I would like to generate a random text on every page load or set it to a low TTL using ESI.

    Is it possible to convert my custom shortcode into an ESI block?

    function display_random_text() {
        // Array of texts
        $texts = [
            "Text 1",
            "Text 2",
            "Text 3",
            // Add more texts here
        ];
    
        // Randomly pick a text
        $random_text = $texts[array_rand($texts)];
    
        // Return the text
        return $random_text;
    }
    
    add_shortcode('random_text', 'display_random_text');
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support qtwrk

    (@qtwrk)

    the simplest way is to change it from [some_shortcode] to [esi some_shortcode ttl="0"]

    Thread Starter senator94

    (@senator94)

    That worked extremely well!

    [esi random_text cache="public" ttl="1"]

    The esi shortcode even works in Elementor’s widgets like Heading, Button, Text Editor.

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to Convert a Shortcode into ESI blocks?’ is closed to new replies.