How to Create WordPress Shortcodes

WordPress

WordPress first introduced shortcodes back in version 2.5 and they have since become an essential component of the content management system. Shortcodes are commonly used in plugins and themes, and WordPress has a number of default codes that allow users to enter something in square brackets – for example – which the CMS will then replace with some other content. It is basically a way to make the running of a website easier and prevent users having to input lengthy strings of code or terms time after time.

Having shortcodes that work with your theme can really speed up a website, so in this tutorial we will show you how you can create your own.

Creating a shortcode

If you have large sections of text or certain terms that you use regularly, a simple shortcode can be created to replace the time-consuming task of entering it repeatedly. Maybe you wanted to add a signature line within a post. Without shortcode, this would require the copy and pasting of the text every time you wanted to use it. Create a shortcode, however, and the task becomes much simpler.

So, using the line of text ‘Hello world!’ as the example, let’s create a shortcode.

To get started you will need to login to your WordPress dashboard and go to Appearance > Editor. Within Editor, find the option for Theme Functions and click on it to access the functions.php file.

In functions.php, you will need to scroll to the bottom of the page, as this is where the shortcode will be added.

Shortcodes consist of two parts: the function and the hook. Here’s how the ‘Hello world!’ shortcode function should be displayed.

function hello_world() {

return 'Hello world!';

}

The function is what our shortcode will be called [hello_world], while the return statement is the text that will appear in our posts ‘Hello world!’ The next thing we need to do is create the hook, which will tell WordPress what to look out for and replace. This will look like this:

add_shortcode('helloworld', 'hello_world');

The two lines of code within the quotation marks are where you specific shortcode code will be added. With our example, ‘helloworld’ is the name of the shortcode and will be entered in a post as [helloworld]. The second part, ‘hello_world’ is the function created in the first step, and tells WordPress to use this function whenever it sees the [helloworld] shortcode.

Click on Update File to save the changes and activate the shortcode.

From here, whenever you write a post in View Mode, you can simply enter your shortcode to display the text or action you require. In this case, [helloworld] displays ‘Hello world!’ And that’s it! That’s how to create shortcodes and make running a website much easier!

Share it!Share on FacebookTweet about this on TwitterShare on Google+Email this to someone

Our Recommended Wordpress Web Hosting Provider

WordPress

WHC.CA
Get a 50% Discount by using our link.

WHC.ca

Reliable host, with excellent customer service and an easy one-click Wordpress installation. Our recommendation to host a Wordpress blog.

Leave a Reply

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