Tuesday , 18 June 2019
Home / Programming / WordPress / Plugins / Creating WordPress Shortcodes – WordPress Plugin Tutorial
Creating WordPress Shortcodes
Creating WordPress Shortcodes

Creating WordPress Shortcodes – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

Shortcodes are very popular in WordPress plugins. Today, we’ll be creating WordPress shortcodes. It’s relatively simple.

Create a new plugin called wpshortcodeex and update the index.php to look like this.

You’ll notice I renamed the function for checking the WordPress version. Reason is we don’t want this plugin to collide with any other functions. So, we give it a unique name. We add WPSCSEX which is short for WordPress shortcode example.

Creating WordPress Shortcodes

Under our action & filter hooks comment, let’s add the following code.

We also include our file shortcodes.inc.php file under the includes section

This is what our shortcodes.inc.php file looks like.

Our jk_WPSCEX_add_message() function takes in 1 parameter, $atts. We then return a message saying to like me on Facebook. Activate your plugin and then create a new post and anywhere you like in the post and place the following code.

Publish your post and then look at it. You should see the message.

Creating WordPress Shortcodes
Creating WordPress Shortcodes

What’s going on?

The add_shortcode() function allows us to add shortcodes. It takes in 2 arguments, the first is the name of your shortcode. This is what the user would type in the square brackets in order to use the shortcode. The next parameter is the name of the function you want to run when someone uses your shortcode.

Our function takes in an argument as well. Which we called $atts, short for attributes. You can actually send data to our function to further extend the functionality of our plugin. We’re not going to talk about this in our current lesson, but the next one. So, don’t worry about it for now if you don’t understand the use for it. For now, just know when it comes to creating WordPress shortcodes, you can extend it with attributes.

Inside our function, we create a variable, $output, that contains our message and then we return that message. . Whenever someone types [jk] or whatever the name of your shortcode is, WordPress will automatically detect it and check if there’s a shortcode function for it. If it does find a function, then it will run that function and will exchange [jk] with whatever is returned from the function.

Do you see how useful shortcodes can be? Anyone who uses your plugin will be able to place the shortcode anywhere they want in their post. They also have the option whether or not they want to user your shortcode. Creating WordPress shortcodes can further be extended by allowing your users to define their own variables and messages. This is just a taste of the WordPress shortcode API. If you would like more info, then please go here.

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

One comment

  1. Sir/Mam,
    when i type [jk], it does not take any effect on page in php tag. Please help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.