How to create a WordPress Shortcode


The shortcode API allows you to create your own shortcodes by adding functions to your theme functions.php template (this can be found; www.your-site.co.uk/wp-content/themes/yourtheme/).

Let’s get straight to the point and start with a basic shortcode function.

Please remember that shortcodes should be created for content and functionality that you use frequently. The whole point of using shortcodes is to save someone time. If you are only going to use something once, there is not much point in creating a shortcode for it.

Let’s create a very simple shortcode that will display today’s date, to do this I would start by adding a function such as this to my theme’s functions.php template:

// Add Shortcode
function shortcode_todays_date() {

// Code
$widget_content  = "Today is " . date("d/m/Y");
return $widget_content;
}
add_shortcode( 'todays_date', 'shortcode_todays_date' );

Those of you who have no coding experience may find the above code a little daunting, however it is easy to understand once you break the code down line by line.

ПОЛЕЗНО  How to Add Admin User to WordPress with MySQL

Even if you have no experience you can go to this website to generate the shortcode code for you.

After saving the functions.php template, we can now call our message whenever we want using the shortcode todays_date.

[todays_date]

The above will output Today is DD/MM/YYYY.

It’s as simple as that, I hope this has helped you.