It’s super easy to add a shortcode to WordPress

I had no idea it was this simple. Basically add a function to functions.php (in a theme folder, like wp-content/themes/yourtheme/functions.php) and add the add_shortcode line as below.

Once it’s been added there, you can use it with [your_shortcode].

For instance, here’s the code I use for generating a random number for the “hit counter” on my main page:

// shortcode to generate a random number
function random_number() {
        // generate a random number
        $number = rand(1000000,10000000); 
        // add commas in the right places
        $number = number_format($number);
        return $number;
}

add_shortcode('random_number', 'random_number');
// now you cause use it with 5,411,455

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.