Quick Wordpress Tip - Building your own shortcode

Quick Wordpress Tip - Building your own shortcode

Wordpress LogoThis is a bit of a break from my normal blogging, but I felt like sharing a quick self-hosted wordpress tip that others might find useful.

In my regular Foto Friday posts, I have a section of content that I copy and paste into each post that points readers to other areas to find my photograph on the web as well as ways to help me with my addiction habit of photography.

The text is:

See more photos in my flickr photostream and/or my 500px portfolio. If you like my photography, feel free to support my addiction habit by visiting Adorama (affiliate link) to purchase new or used photographic gear. Or, if you are interested in renting gear before buying, try out my favorite camera, lens and gear rental site –  Borrowlenses.com (affiliate link) – the folks at Borrowlenses are awesome.  All proceeds from clicking the above affiliate links go to more photographs. 

For each Foto Friday post, I would go back to the previous post and copy the text and then paste it into the new post. While not difficult, this was getting to be a chore for me….and I started wondering how to make it more automated.

I’ve built themes and custom functions for WordPress before so I knew about shortcodes….but hadn’t really used a shortcode for just adding text before…but I figured it would work just fine….and it did.

This is a fairly easy thing to do for those of you who are technically inclined.  All you need to be able to do this yourself is a self-hosted version of WordPress and access to your theme’s “functions.php” file – and some basic HTML (and perhaps PHP) knowledge.

Building a Custom Shortcode

I found my theme’s functions.php file a pulled it off my server to edit it.  Using Notepad++ (create free editor for Windows), I added the following lines:

[sourcecode language=”html”]
/* Add shortcode */
add_shortcode(‘phototext’, ‘addPhotoText’);
[/sourcecode]
The above is the definition of the shortcode text to use. In the add_shortcode function, the first field ‘phototext’ is my actual shortcode text that I want to use in each post. The second field ‘addPhotoText’ is the name of the function that should be run whenever the shortcode ‘phototext’ is found. Pretty simple so far, no?

Now…the code below is the actual ‘addPhotoText’ function. The only thing I want this function to do is insert the HTML code listed below into a post whenever the ‘phototext’ shortcode is found.

[sourcecode language=”html”]
/* Function for added shortcode */
function addPhotoText() {
return ‘
See more photos in <a href="http://www.flickr.com/photos/ericbrown">my flickr photostream</a> and/or my <a href="http://500px.com/ericbrown">500px portfolio</a>. If you like my <a href="https://ericbrown.com/photography">photography</a>, feel free to support my <del>addiction</del> habit by visiting <a href="http://www.adorama.com?kbid=65146">Adorama</a> (affiliate link) to purchase new or used photographic gear. Or, if you are interested in renting gear before buying, try out my favorite camera, lens and gear rental site – <a href="http://www.borrowlenses.com/?blpid=photographyminute">Borrowlenses.com</a> (affiliate link) – the folks at Borrowlenses are awesome. All proceeds from clicking the above affiliate links go to more photographs.
‘;
}
[/sourcecode]

Once the above shortcode and function have been added to the functions.php file, I uploaded the file to my test server to make sure i didn’t fat-finger something. Once the new shortcode was confirmed to work, I moved it over here to my blog.

So now…anytime i type my shortcode phototext wrapped in brackets (a shortcode is wrapped in brakcets – e.g., [shortcode]), I’ll get the following added to any post/page:

[phototext]

Shortcodes make life much easier for WordPress users. If you have some content that you like to re-use (author bio, etc), take a look at using shortcodes…they may save you time.

BTW – If you have any questions about WordPress, shortcodes or anything else, drop me a line and I’ll see what I can do to help.