What is WordPress Shortcode? – Shortcode Examples

If you have been using WordPress for a while, you might have heard the term “Shortcode.” But What is WordPress Shortcode, anyway? In this article, we are going to learn about the basics of Shortcode in WordPress.

What is WordPress Shortcode?

A Shortcode is a small block of code that lets you add features to your WordPress with coding or technical knowledge. A shortcode is a convenient & easy way to add various features. Generally, a shortcode is indicated by square brackets [shortcode].

Let us understand more about  Shortcodes in WordPress –

Shortcode makes the website content clean and structured. Although you can easily insert or write basic HTML in your posts, you cannot add PHP code to your posts. Shortcodes are also used on the sidebar area of many WordPress websites.

WordPress makes sure that post or page content doesn’t contain any malicious code, Meaning that putting PHP code to post is very complicated or impossible. 

 And this is where Shortcodes come in. 

Shortcode API lets you insert custom code to your post without any issue. With shortcodes, you can easily add things like popular posts, contact forms, galleries, etc.

Shortcodes are basically PHP functions written by developers. And that PHP function is implemented using shortcode defined by that developer. As discussed earlier, creating a shortcode require coding knowledge but using a shortcode doesn’t require any coding knowledge. 

You can insert a shortcode anywhere on your WordPress site. But remember one thing, if a shortcode comes with a plugin or a theme, deleting the plugin or theme will make the shortcode invalid (in other words, that particular shortcode will not work anymore). WpBakery Page builder uses shortcodes.

WordPress Shortcode Examples : audio , video, gallery,etc.

Built-in Shortcodes in WordPress

WordPress provides some built-in Shortcodes – 


audio: play and embed audio files.

video: play and embed video files.

gallery: display images as a gallery.

playlist: Display collection of video and audio files

embed: Wrap Embedded items

caption: Add caption around content.

Source: WordPress

Also Read : Code Snippets in WordPress: An Easy Detailed Guide 2021

How to Create Shortcode in WordPress

How to Create Shortcode in WordPress

If you know programming and a little bit of PHP, you can easily create Shortcode in WordPress. 

To create a custom shortcode, Follow the steps given below. 

1. Either create a new PHP file inside the same folder as your theme’s functions.php file or You can add them to the functions.php file. However, I would recommend keeping a separate file for custom shortcodes. (here, we are going with a new file).

2. Name the file “added_shortcodes.php or something.php, now start writing code inside that file. but before you do that, make sure to include your new file to functions.php by pasting the following code –  

include(‘added_shortcodes.php’);

What is WordPress Shortcode?

Note: You have to paste this code to the functions.php to make the shortcode work.

3. Now write the code, Here is a sample function to create a custom shortcode to get the current year using shortcode 2022.

	function year_shortcode() {
	$year = date('Y');
	return $year;
	}
	add_shortcode('year', 'year_shortcode');

Source: CSS-Tricks

4.add_shortcode ( shortcode handler function) is a built-in function of WordPress and it takes to parameters , 1.the shortcode tag ($tag) and 2.the corresponding function ($func). By using the the shortcode tag you can execute that specific function.

Demo structure of a Shortcode function in WordPress:

function do_something(
function body / statement;
)
add_shortcode('shortcode_tag',' do_something');

5. Usage : [shortcode_tag]

To use the custom shortcode, just paste your shortcode tag (1st parameter of add_shortcode function). Use it with square brackets as shown above.

And you’re done! Now you know how to create shortcodes in WordPress easily.

Note: Naming convention for shortcode tags – Don’t use hyphens and user lowercase characters only, You can also use underscores).

Learn More about creating custom shortcodes

5 Top WordPress Shortcodes Plugins 

These are some of the best WordPress Shortcodes Plugins. 

  1. Shortcodes Ultimate
  2. WP Shortcode by MyThemeShop
  3. Shortcoder
  4. Meks Flexible Shortcodes
  5. Arconix Shortcodes

Conclusion: 

I hope that you got the idea of “What is WordPress Shortcode.” Now you can easily create simple shortcodes and use them. 

Images Credit: undraw,pexels

Leave a Comment