phpwordpressshortcode

Why is my WordPress plugin shortcode output duplicating every time I refresh the page?


I'm building a simple WordPress plugin that fetches a random quote from an external API and displays it using a shortcode.

The problem is: every time I refresh the page, the quote seems to duplicate — it appears twice (or more) inside the same container. I expected it to show just one quote per page load.

Here’s a simplified version of my code:

function my_quotes_shortcode() {
       $response = wp_remote_get('https://api.quotable.io/random');
       if (is_wp_error($response)) {
               return 'Quote unavailable.';
       }
      
       $data = json_decode(wp_remote_retrieve_body($response));
       $quote = $data->content ?? 'Default quote';
       $author = $data->author ?? 'Unknown';
       return "<div class='quote-box'>\"{$quote}\" - {$author}</div>";
}

add_shortcode('daily_quote', 'my_quotes_shortcode');

What I Tried

What I Expected

Notes

Is this related to how WordPress handles shortcode rendering or something with API timing? Any help would be appreciated!

Update: The duplication was caused by the shortcode being executed multiple times in certain Gutenberg block conditions.

Using a static variable inside the shortcode function solved it, thanks to the answers provided below!


Solution

  • The problem is most likely caused by a request for a missing favicon.ico file. you can add a site icon through the WordPress Customizer, which handles this for you:

    Go to Appearance → Customize.

    Navigate to Site Identity.

    Under Site Icon, upload an image. WordPress will generate the necessary favicon files and link them in the header.

    Once a valid favicon is found, the browser will stop making the 404 request that causes the double-fire.

    You can also make your shortcode "execution-aware" so it can't run more than once per page load, regardless of the cause.

    function my_quotes_shortcode() {
        // 1. Add a static variable to track execution.
        static $did_execute = false;
    
        // 2. If the function has already run during this request, stop.
        if ( $did_execute ) {
            return ''; // Return an empty string to prevent duplicates.
        }
    
        // 3. Set the flag to true so it won't run again.
        $did_execute = true;
    
        // Your original, correct code continues here.
        $response = wp_remote_get( 'https://api.quotable.io/random' );
    
        if ( is_wp_error( $response ) ) {
            return 'Quote unavailable.';
        }
    
        $data   = json_decode( wp_remote_retrieve_body( $response ) );
        $quote  = $data->content ?? 'Default quote';
        $author = $data->author ?? 'Unknown';
    
        return "<div class='quote-box'>\"{$quote}\" - {$author}</div>";
    }
    add_shortcode( 'daily_quote', 'my_quotes_shortcode' );