Creating Shortcodes

 

Now it is time to dive into another technical plugin and help you understand how plugins work at an operational level. What is a shortcode? A shortcode is a symbol or marker that gets replaced by html code during the rendering of a wordpress page. Here is a typical example. At some point on your webpage, you have probably added a standard contact form. If you insert one, then switch from the “visual” to the “text” tab, you’ll see something like this:

 

1

 

 

This is the shortcode for the contact me form. Not so short, you think? Then compare it to the html code that takes its place when the page renders:

 

2

 

 

So you see, even though the shortcode might still look like computer code, it’s minimal compared to real computer code, it’s code you can actually type and work with. That’s the beauty of wordpress: all the complexity is hidden and a minimal amount of control is left to you to configure a working site.

If this intro hasn’t frightened you away, I strongly encourage you to stick with me as I teach you how to create your own custom shortcodes. If you’re on the fence, scroll to the bottom and see a really cool trick you’ll be able to do after wading through the geeky mumbojumbo. First, install the Shortcoder Plugin. Let’s start with something simple. There may be a page or a series of pages that you want to have a common footer, kind of like an email signature, and you’ve copy-pasted something over and over again. For example, on each of my download pages, I have the following icons & message. Looks nice, but I have over twenty download pages, and I was copy/pasting this footer each time.

3

 

Go to Settings > Shortcoder and you will see something beautiful. It’s exactly the message you see at the bottom of each of my downloads pages. So the plugin allows me to create it once, then reference it in any page I want. Any changes I make to this centrally maintained code is reflected on every page.

 

4

 

 

Here is what I put at the bottom of each download page. (Notice above where it says “your shortcode is.”)

5

 

WordPress, as it renders each page, scans for this shortcode and replaces it with the message stored in the settings. That’s the concept. Now let me show you something truly cool. Shortcodes are more than just placeholders, they are also customizable. Take another look at my download page. See the goodreads reviews at the bottom? Wouldn’t you love to add this little widget to your book pages?? Here’s how I did it. At the bottom of each download page there appears the following shortcode:

 

6

 

 

Notice that in addition to the shortcode itself, there are three parameters: the book’s goodreads id, isbn, and title. Going back to Settings > Shortcoder, here’s how the plugin manages those parameters:

 

7

 

 

As wordpress renders the page, it looks out for this goodreads shortcode, and replaces each instance of it with the above code. But it also replaces each instance of %%id%% within that code with the book’s goodreads id. For those of you who don’t read html (we are writers after all, not coders), what this snippet of code does is insert a frame which goodreads fills with all the reviews for your book as they appear on your book’s goodreads page. I don’t expect you to type all that, after all, I copy/pasted it myself out of goodreads’ developer area. I can’t post it directly here because this website is itself a wordpress site which will try to render the code rather than just display it, so click here to access a copy for yourself. Please note though, that buried in the code is a place for “developer id.” No, you do not have to register as a goodreads developer in order for this to work, but keep in mind that like any other big site, if you use their tools without registering, they’ll throttle you after so many tens of thousands of clicks. Here is the final result, tailored to each individual book:

 

8

 

The power of this one little plugin is so awesome, you can just about use it to create your very own plugins. If you have any questions about shortcodes, please contact me.

 

Find more writing and publishing tips at Nothing Any Good.

 

 

Victor Davis is the author of one short story collection, Grains of Sand, and is publishing a second book, The Gingerbread Collection, in the spring of 2016. He shares his writing and reading adventures at his blog Mediascover. You can find him on FacebookTwitter, and Goodreads. His Books & Stories are available on AmazonApple iBooksSmashwords, and Barnes & Noble.

 

Leave a Reply

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