Scrapbook / Web Design & Devleopment

Make your own WordPress shortcodes

Using shortcodes in your WordPress sites can massively benefit your clients by giving them a simple way of adding content or functionality they may not know how to do themselves.

Make your own WordPress shortcodes to make your clients lives easier.

What can you use shortcodes for?

Sortcodes have been used in many wordpress site to achieve many different things, common uses include embedding media such as tweets and YouTube videos. A personal favorite use for shortcodes for a grid system. That’s a great way to give people the ability to split content up into however many columns they want without being confined the the simple editor provided with WordPress.

Below is a basic example.

<?php
/**
 * adds a shortcode called [loremipsum] and when used will out put the content in the function above.
 */
function lorem_ipsum() {
	return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec nulla vitae lacus mattis volutpat eu at sapien. Nunc interdum congue libero, quis laoreet elit sagittis ut. Pellentesque lacus erat, dictum condimentum pharetra vel, malesuada volutpat risus. Nunc sit amet risus dolor. Etiam posuere tellus nisl. Integer lorem ligula, tempor eu laoreet ac, eleifend quis diam. Proin cursus, nibh eu vehicula varius, lacus elit eleifend elit, eget commodo ante felis at neque. Integer sit amet justo sed elit porta convallis a at metus. Suspendisse molestie turpis pulvinar nisl tincidunt quis fringilla enim lobortis. Curabitur placerat quam ac sem venenatis blandit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nullam sed ligula nisl. Nam ullamcorper elit id magna hendrerit sit amet dignissim elit sodales. Aenean accumsan consectetur rutrum.';
}
add_shortcode('loremipsum', 'lorem_ipsum');
?>

When you use the shortcode [loremipsum] it will output the content within the function above. You can take this a step further and use it to wrap content within a div.

<?php
/**
 * adds a shortcode called [loremipsum]. You can use it in the following way [loremipsum] *content goes here* [/loremipsum]
 */
function lorem_ipsum( $attr, $content = null ) {
    return '<div class="loremipsum">' . $content . '</div>';
}
add_shortcode('loremipsum', 'lorem_ipsum');
?>

If you put content between the open and closing shortcode it will be outputted between the open and closing div in the function above.

You can use shortcodes for other things such as social sharing buttons, embedding tweets, outputting YouTube videos and so on. These examples can be found all over the web. I highly suggest using the WordPress documentation to learn more about the use of shortcodes

Conclusion

Shortcodes can provide an array of different functionality to make tasks easier for a non-technical user. This is really simple to do, and is a really good way of increasing the usability of a sites admin area.

What next?

Check out some of my work!

I'm constantly working on new and exciting projects. Check out my web design and development portfolio with write-ups on how each project was conducted. Looking for some great photography? Check out my latest photography articles or browse the whole colection. Finally, I love to write. I write about everything from the web and photography to many other subjects that interest me.