Creating Custom WordPress Shortcodes

  • follow us in feedly
Published February 16, 2013 by Brad Knutson
WordPress Shortcodes

WordPress shortcodes can be used to quickly display content that you use over and over again. All standard WordPress functions are available to you, which makes building dynamic blocks simple.

The structure for coding your own shortcode is below. Add your function to your functions.php file in your theme.

How to use the newly created shortcode

You can now use your new shortcode in any page or post. Just type [customshortcode] into the visual or HTML editor.

The possibilities are endless. Here are a few examples.

Simple Custom Shortcode Example

I then use the [helloworld] shortcode in my post and get the below result:

hello world!!!

Advanced Custom Shortcode Example

Let’s say that rather than using a WordPress plugin to display info about an author, you want to build a custom shortcode and insert it into only certain posts. Perhaps we want the author’s avatar, and links to the last three posts they’ve made. The below shortcode function will do just that. I’ve added a little bit of inline style to make it look nice.

Now all I have to do is punch [author_post] into the page and let the function do the work.

That wasn’t that hard, right? With the full army of WordPress functions at your disposal, pretty much any situation you can think of can be built into a shortcode. You can build functions for forms, post archives, author bios, image galleries, you name it.

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date

Subscribe

Topics

See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">