Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

WordPress ShortcodeIf you are like me … I am always looking to make things faster and easier in terms of website development and programming. Whether it’s something really basic and simple, or a little more advanced. It doesn’t matter to me. If it can save time I am usually all for it.

Not only with WordPress, but that includes any CMS PHP script, system programming, or what have you. As long as I don’t have to spend hours upon end to make it work because at some point I have to draw the line, and say is it really worth it?

I have used a lot of PHP CMS Scripts in my day, and I have to admit WordPress is much easier by far to customize compared to the majority of others I have worked with. It’s shortcode ability feature is definitely no exception.

Why Use Shortcode?

  • When there is something that you want to reuse on a regular basis.
  • When you want to reuse the same snippet of code, text, etc.
  • When you want to use php, or other code in a post/page that may get stripped.


What you might do with it:

  • Use it to add an about me section to your blog posts.
  • Display different ad sizes or formats on various sections of your website.
  • Use code such as php in a blog post.
  • And much, much more!

Really, there are so many possible scenarios where shortcodes would come in very handy.

 

Basic WordPress Shortcode Example:

Call it in a page or post with:

Or, manually add it to a static .php theme file with:

That basic example will just add and output:
Be sure to check this out!

Wherever you add: [name_of_my_shortcode] in the WordPress Dashboard Editor.

 

How To Create Custom WordPress Shortcodes:

WordPress

There are several ways that you can create your own shortcode. One of the easiest is to add a snippet of code to your theme functions.php file. However, if you update your theme anything that you add to functions.php will be lost (wiped out) after a theme update. To prevent losing custom code in functions.php consider creating a WordPress Child Theme, or even better create your own custom plugin. Both are definitely worth the time to do.

Open functions.php with either your hosts file manager editor, or directly in the WordPress Administration Dashboard under:
-> Appearance
–> Editor
—> Theme Functions (functions.php)

At the end of the file add:

Then, save the changes.

That’s it.

Now just paste the shortcode in the WordPress blog post or page post editor with:

That’s just a very basic example of what you can do and how to do it.

 

Create An About Me Section Using Shortcode:

Now let’s create a custom About Me section at the end of a blog post.

This time we will add the following to functions.php instead:

Then, add to end of your blog post:


Note:

If you create shortcode with a lot of html, then use this instead:

Complete Example With All The Custom Code:

Then, at the bottom of your blog post (or, anywhere else that you want it displayed) add:

About Me Shortcode

 
And, it will look like this in a blog post:

About Me Shortcode Output

Obviously you would add your own html and styling to suit your needs. Even use custom style.css and classes/ids, etc. as well if you want.

Now instead of having to switch from the visual editor to the text editor, and having to paste all that html and style code into the area where you want to display it all you have to do is paste one short and simple line of shortcode in the WordPress editor.

If you want to change it you would edit the code in functions.php one time, and anywhere the shortcode was added would all change.

This is really fast and easy to accomplish, and if you ever want to make changes to the code it will save you a lot of time as you wouldn’t have to go through and edit a bunch of posts one by one.

You could easily use shortcode to add various advertisements on your site too. Just use a different function name and shortcode name for each one.

For example add this to functions.php as well:

Then, to use it in a blog post or page post paste this in the WordPress editor:

Or add it to a static .php theme file for example (header.php, footer.php, sidebar.php, etc.):

The possibilities are enormous when you think about it.

But again, be sure to consider a child theme or custom plugin to prevent losing your shortcode whenever there is an update for your theme.

Yes, you could install a plugin to create shortcode. Maybe another for managing ads, and another to do something else. Why install a bunch of additional plugins when you can easily accomplish this without them.

This obviously isn’t for everyone, especially if you are not that technical or good with code. However, if you are you can do some really nifty stuff with it. Not to mention save yourself a lot of time if used right.

About: Jeremy LeSarge - AKA: Ray (211 Posts)

I am the site owner and administrator of DialMe.com. I provide help and tips for Boonex Dolphin on the main part of this website where you will also find an assortment of other resources. Here, on the blog I write about a variety of topics surrounding WordPress, technology, social media/networking, SEO, and webmaster resources.