# WordPress Shortcodes: The Complete Guide

- Link: https://tomas-shejbal.cz/en/wordpressito/wordpress-shortcodes-the-complete-guide/
- Published: 2026-03-05T12:11:12+01:00
- Author: tom

# WordPress Shortcodes: The Complete Guide

A shortcode lets you insert dynamic content into pages and posts.

A developer can create a shortcode once and website users can then insert it anywhere
on the site.

Shortcodes let you add many useful features to your WordPress site without you, 
as a user, having to deal with PHP. By creating custom shortcodes, you can simplify
content management and add custom functionality to your site.

And what can go inside a shortcode? Pretty much anything — from outputting a single
word to loading a booking form.

![WordPressito - Shortcodes](https://tomas-shejbal.cz/wp-content/uploads/2026/03/
wordpressito_shortcodes.webp)

_Last updated: 4 March 2026_

_Note: More advanced users can create their own shortcodes. You can read this article
as an overview of what's possible — or dive deeper and create your first shortcode.
At the end of the article I show how to create your own plugin where you register
the shortcode._

## 1. Creating a simple PHP function

First, let's see how to create a simple PHP function that adds two numbers together.

    ```wp-block-code
    function my_simple_addition($a, $b) {
        $sum = $a + $b;
        echo "The sum of $a and $b is: $sum";
        return $sum;
    }
    ```

This function takes two parameters (`$a` and `$b`), adds them together, outputs 
the result, and also returns the sum value.

If you wanted to use this function in a PHP template / custom plugin / PHP snippet,
you would call it like this:

    ```wp-block-code
    <?php my_simple_addition(5, 3); ?>
    ```

The result shown on the site where the code is inserted would be: "The sum of 5 
and 3 is: 8".

A regular user won't be calling PHP functions directly, though. Let's solve that.

## 2. Creating a basic shortcode without arguments

We'll turn the function into a shortcode that can be used directly in page or post
content — no PHP knowledge needed. We'll start with a simple shortcode that has 
no arguments.

    ```wp-block-code
    // Simple function to demonstrate a shortcode without arguments
    function simple_addition_shortcode() {
        // For simplicity, we'll add two fixed values
        $a = 5;
        $b = 3;
        $sum = $a + $b;

        return "The sum of $a and $b is: $sum";
    }

    // Register the shortcode
    add_shortcode('add_basic', 'simple_addition_shortcode');
    ```

You can use your custom shortcode anywhere on the site using the WP _Shortcode_ 
block:

    ```wp-block-code
    [add_basic]
    ```

WordPress will replace it with the text: "The sum of 5 and 3 is: 8"

> _Note: Notice that the shortcode uses return instead of echo. This is because 
> shortcodes must return the content that should be inserted into the page._

## 3. Adding arguments to a shortcode

Now let's improve our shortcode so it can accept input arguments, just like the 
original function did.

    ```wp-block-code
    // Function to add two numbers
    function my_simple_addition($a, $b) {
        $sum = $a + $b;
        return "The sum of $a and $b is: $sum";
    }

    // Shortcode function with arguments
    function addition_shortcode($atts) {
        // Define default values for the attributes
        $atts = shortcode_atts(array(
            'a' => 0,
            'b' => 0,
        ), $atts, 'add');

        // Convert values to numbers for correct addition
        $a = floatval($atts['a']);
        $b = floatval($atts['b']);

        // Return the result of the function
        return my_simple_addition($a, $b);
    }

    // Register the shortcode
    add_shortcode('add', 'addition_shortcode');
    ```

You can now use the shortcode with custom values in your pages or posts. I've also
renamed the shortcode to "add":

    ```wp-block-code
    [add a="10" b="20"]
    ```

This shortcode will output: "The sum of 10 and 20 is: 30"

## 4. A closer look at shortcode_atts()

The `shortcode_atts()` function is key to working with shortcode arguments. It takes
three parameters:

 1. **Default values** — an associative array of default values for the input arguments
 2. **Shortcode arguments** — the values entered by the user
 3. **Shortcode name** — used for filtering (optional)

    ```wp-block-code
    $atts = shortcode_atts(array(
        'a' => 0,  // Default value for argument 'a' is 0
        'b' => 0,  // Default value for argument 'b' is 0
    ), $atts, 'add');
    ```

This function sets default values for any arguments the user didn't provide.

## 5. Practical tips and recommendations

### Where to put the code

You can place your shortcode:

 1. In a _custom plugin_ — the recommended approach.
 2. In _functions.php_ of a _child theme_ — less ideal.
 3. In a _PHP snippet_ via a plugin like Code Snippets — the least recommended option.

### Creating a custom plugin for shortcodes

    ```wp-block-code
    <?php
    /**
     * Plugin Name: My Custom Shortcodes
     * Description: A plugin containing useful shortcodes for the site
     * Version: 1.0
     * Author: Your Name
     */

    // Shortcode for addition
    function my_simple_addition($a, $b) {
        $sum = $a + $b;
        return "The sum of $a and $b is: $sum";
    }

    function addition_shortcode($atts) {
        $atts = shortcode_atts(array(
            'a' => 0,
            'b' => 0,
        ), $atts, 'add');

        $a = floatval($atts['a']);
        $b = floatval($atts['b']);

        return my_simple_addition($a, $b);
    }
    add_shortcode('add', 'addition_shortcode');

    // You can add more shortcodes here
    ```

### Watch out for name conflicts

Shortcode names must be unique, otherwise they can conflict with other plugins. 
Consider adding a prefix specific to your site or plugin:

    ```wp-block-code
    add_shortcode('mysite_add', 'addition_shortcode');
    ```

### Using a shortcode in PHP code

If you need to use a shortcode inside PHP code (for example in a template), use 
the `do_shortcode()` function:

    ```wp-block-code
    <?php echo do_shortcode('[add a="10" b="20"]'); ?>
    ```

## Need a shortcode?

Need a shortcode but don't feel like writing it? Get in touch and we'll figure something
out. 😉
