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.

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.
First, let's see how to create a simple PHP function that adds two numbers together.
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:
<?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.
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.
// 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:
[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.
Now let's improve our shortcode so it can accept input arguments, just like the original function did.
// 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":
[add a="10" b="20"]
This shortcode will output: "The sum of 10 and 20 is: 30"
The shortcode_atts() function is key to working with shortcode arguments. It takes three parameters:
$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.
You can place your shortcode:
<?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
Shortcode names must be unique, otherwise they can conflict with other plugins. Consider adding a prefix specific to your site or plugin:
add_shortcode('mysite_add', 'addition_shortcode');
If you need to use a shortcode inside PHP code (for example in a template), use the do_shortcode() function:
<?php echo do_shortcode('[add a="10" b="20"]'); ?>
Need a shortcode but don't feel like writing it? Get in touch and we'll figure something out. 😉