Using WordPress’ Shortcode System

/ Wordpress / by Paul Robinson / 3 Comments

Today I’m going to show you how to write your own shortcode plugin for WordPress. The shortcode system can be extremely powerful & for developers they are great for allowing clients to insert complicated pieces of HTML into posts without worrying about it being overly complicated.

Now as usual you need to start your plugin with the following:

You only really need the name and it is good practice to keep the version number. I would always advice keeping all of them though as it’s always good to get into the practice of having them all for when you make the next awesome premium plugin.

Ok, so now we can tell WordPress about our new shortcode. To do this we use one simple line:

tagname is the name of the tag used, so in this case it would be [tagname] and function_name is the name of the function that is called when the tag is used.

Now we need to tell WordPress what to do when it finds our shortcode. The shortcode I am going make as an example is somewhat pointless, but it is easy to understand and yet still shows some of the useful things shortcodes can do.

There you go, a fairly simple shortcode that will generate a hyperlink where ever the [link] shortcode is used. Its usage would be something like this:

That will make a standard hyperlink. You may have noticed in the code that I added a lightbox attribute, that allows you to define a gallery for the lightbox script.

I hope this little tutorial helps you with how to start working with the WordPress shortcode system. The shortcode I wrote here is not particularly useful but it does give a basic idea of how to make shortcodes. Everything else extends from this point on. For instance a shortcode can also be written without a closing tag and so does not have to depend on the content inside the tags, such as the WordPress Gallery shortcode.

As always, if you have any questions please feel free to ask them in the comments.

3 Comments

Author’s gravatar

Hey,

Looks like an old post, but after a few hours of searching through useless google posts this one actually helped!

Anyway I’ll be making some shortcodes now, bye.

Reply
Older Comments
Newer Comments

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.

I'll keep your WordPress site up-to-date and working to its best.

Find out more