How to Create Your Own WordPress ShortCodes

Wordpress Shortcodes

Shortcodes were introduced in WordPress version 2.5. It also comes bundled with many plugins and themes that support Shortcodes. The shortcode API lets you create your own shortcodes to add custom functionality to your WordPress website. Shortcodes are a small piece of text inside square brackets which are replaced with content in post and page. In WordPress based websites, you are not allowed to add PHP codes in posts and pages. But you can create custom functions and then shortcodes of those functions to run within the pages. You can use shortcodes at any place where you can add text in WordPress.

The shortcode API lets you create your own shortcodes to add custom functionality to your WordPress website. WordPress scans and filters the content to make sure no-one is injecting a malicious code in the database. So, you cannot write PHP code in posts or pages. If you want to add something that requires code injection, you need shortcodes. It is used to display related posts, banner ads, contact forms, galleries, etc in posts.

If you are new to WordPress and do not know how to create shortcodes for your function, you are at the right place. In this post, we will see how to create shortcodes for a custom function.

WordPres Shortode

For making shortcodes, you either need to add the code in functions.php file of your theme or make a side wide plugin.

Before creating a shortcode, you need to create a call back function. This function will be called each time the shortcode will encounter in post or page. Open functions.php file of your theme and add the sample code. mycustomFunction is the callback function for shortcode Myfn.

function mycustomFunction()
return '<h3>My Function</h3>';
add_shortcode('Myfn', 'mycustomFunction'); // this will create shorcode for the function mycustomFunction

Whenever you want to add the mycustomFunction within post or page, just add [Myfn] shortcode. It will add <h3>My Function</h3> in place of [Myfn] on the page.

Sometimes, there is a need for passing values in the function. So, we have to create shortcodes that can pass value. See the example given below:

function gallery_pic($attributes) {
'width' => 450,
'height' => 210,
), $attributes));
return '<img src="'. $width . '/'. $height . '" />';
add_shortcode('showpicture', 'gallery_pic');

Here we have made a shortcode for adding an image with custom height and width values.

When you use this shortcode [showpicture] to show the image. It will put the image with default height and width of 450 x 210 pixels. You can also use this shortcode by passing values of height and width.

[showpicture width="300" height="250"]

Now it will add the image with height and width of 300 x 250.

With shortcodes, you can add complex functionality to WordPress posts and pages. The most common use it the addition of gallery and form. Almost all gallery and form plugins use shortcode feature and make it easy to add a gallery or form in the pages or post.

Try it by yourself and comment below if you are facing any problem.

Tags: |

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.

Similar Articles


Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker