Adding Column Layout Shortcodes to Your Wordpress Theme

Posted on 19. Sep, 2010
by Tutorials Etc in Plugins, Wordpress

In this tutorial, we'll show you how to add column shortcodes to your Wordpress theme. In the end, we also cover how to convert this into a column-layouts plugin.

Column Shortcodes Adding Column Layout Shortcodes To Your Wordpress Theme

Shortcodes have become almost a requirement in Premium Wordpress Themes and their use is on the rise.

This is the first tutorial of many in which we will explain the implementation of the shortcodes we used in our Premium Wordpress Themes inFocus and Awake.

Since there is already a wealth of documentation on the intricacies of how shortcodes work, we will be focusing primarily on their implementation. If you’d like to learn more about shortcodes in general, we recommend the following resources:

1. Wordpress Codex – Shortcode API
2. Mastering WordPress Shortcodes

The following shortcodes allow the end user to create percentage-based infinitely nestable column layouts.
Preview the end result: Awake Column Shortcodes

You can download a plugin version of this tutorial at the bottom of this post.

Step #1 – The PHP Code

Add the following php code to your theme’s functions.php file. If your theme doesn’t have a functions.php file create one and paste in the following:

function webtreats_one_third( $atts, $content = null ) {
   return '<div class="one_third">' . do_shortcode($content) . '</div>';
}
add_shortcode('one_third', 'webtreats_one_third');

function webtreats_one_third_last( $atts, $content = null ) {
   return '<div class="one_third last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('one_third_last', 'webtreats_one_third_last');

function webtreats_two_third( $atts, $content = null ) {
   return '<div class="two_third">' . do_shortcode($content) . '</div>';
}
add_shortcode('two_third', 'webtreats_two_third');

function webtreats_two_third_last( $atts, $content = null ) {
   return '<div class="two_third last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('two_third_last', 'webtreats_two_third_last');

function webtreats_one_half( $atts, $content = null ) {
   return '<div class="one_half">' . do_shortcode($content) . '</div>';
}
add_shortcode('one_half', 'webtreats_one_half');

function webtreats_one_half_last( $atts, $content = null ) {
   return '<div class="one_half last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('one_half_last', 'webtreats_one_half_last');

function webtreats_one_fourth( $atts, $content = null ) {
   return '<div class="one_fourth">' . do_shortcode($content) . '</div>';
}
add_shortcode('one_fourth', 'webtreats_one_fourth');

function webtreats_one_fourth_last( $atts, $content = null ) {
   return '<div class="one_fourth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('one_fourth_last', 'webtreats_one_fourth_last');

function webtreats_three_fourth( $atts, $content = null ) {
   return '<div class="three_fourth">' . do_shortcode($content) . '</div>';
}
add_shortcode('three_fourth', 'webtreats_three_fourth');

function webtreats_three_fourth_last( $atts, $content = null ) {
   return '<div class="three_fourth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('three_fourth_last', 'webtreats_three_fourth_last');

function webtreats_one_fifth( $atts, $content = null ) {
   return '<div class="one_fifth">' . do_shortcode($content) . '</div>';
}
add_shortcode('one_fifth', 'webtreats_one_fifth');

function webtreats_one_fifth_last( $atts, $content = null ) {
   return '<div class="one_fifth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('one_fifth_last', 'webtreats_one_fifth_last');

function webtreats_two_fifth( $atts, $content = null ) {
   return '<div class="two_fifth">' . do_shortcode($content) . '</div>';
}
add_shortcode('two_fifth', 'webtreats_two_fifth');

function webtreats_two_fifth_last( $atts, $content = null ) {
   return '<div class="two_fifth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('two_fifth_last', 'webtreats_two_fifth_last');

function webtreats_three_fifth( $atts, $content = null ) {
   return '<div class="three_fifth">' . do_shortcode($content) . '</div>';
}
add_shortcode('three_fifth', 'webtreats_three_fifth');

function webtreats_three_fifth_last( $atts, $content = null ) {
   return '<div class="three_fifth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('three_fifth_last', 'webtreats_three_fifth_last');

function webtreats_four_fifth( $atts, $content = null ) {
   return '<div class="four_fifth">' . do_shortcode($content) . '</div>';
}
add_shortcode('four_fifth', 'webtreats_four_fifth');

function webtreats_four_fifth_last( $atts, $content = null ) {
   return '<div class="four_fifth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('four_fifth_last', 'webtreats_four_fifth_last');

function webtreats_one_sixth( $atts, $content = null ) {
   return '<div class="one_sixth">' . do_shortcode($content) . '</div>';
}
add_shortcode('one_sixth', 'webtreats_one_sixth');

function webtreats_one_sixth_last( $atts, $content = null ) {
   return '<div class="one_sixth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('one_sixth_last', 'webtreats_one_sixth_last');

function webtreats_five_sixth( $atts, $content = null ) {
   return '<div class="five_sixth">' . do_shortcode($content) . '</div>';
}
add_shortcode('five_sixth', 'webtreats_five_sixth');

function webtreats_five_sixth_last( $atts, $content = null ) {
   return '<div class="five_sixth last">' . do_shortcode($content) . '</div><div class="clearboth"></div>';
}
add_shortcode('five_sixth_last', 'webtreats_five_sixth_last');

Step #2 – The CSS Code

Fluid Columns

When you’re building a site for yourself or a client, you have the luxury of using fixed column layouts like we did in our first theme inFocus.

But, in premium theme development, users will often want to change the width of the site, the width of the sidebar, nest the column shortcodes and use them within other structures that throw the dimensions off.

For this reason, we implemented fluid columns in our second theme Awake. You can see in the examples below how much more flexible they are.

http://themes.mysitemyway.com/awake/shortcodes/column-shortcodes/
http://themes.mysitemyway.com/awake/features/layouts/column-layouts/

The CSS

Insert the following CSS in your themes style.css file.

/* ------- Fluid Columns ------- */
.one_half{ width:48%; }
.one_third{ width:30.66%; }
.two_third{ width:65.33%; }
.one_fourth{ width:22%; }
.three_fourth{ width:74%; }
.one_fifth{ width:16.8%; }
.two_fifth{ width:37.6%; }
.three_fifth{ width:58.4%; }
.four_fifth{ width:67.2%; }
.one_sixth{ width:13.33%; }
.five_sixth{ width:82.67%; }
.one_half,.one_third,.two_third,.three_fourth,.one_fourth,.one_fifth,.two_fifth,.three_fifth,.four_fifth,.one_sixth,.five_sixth{ position:relative; margin-right:4%; float:left; }
.last{ margin-right:0 !important; clear:right; }
.clearboth {clear:both;display:block;font-size:0;height:0;line-height:0;width:100%;}

Step #3 – Disabling Wordpress wpautop and wptexturize filters.

By default Wordpress’s wpautop and wptexturize filters inject p tags and br tags around the outputted column content which is a problem if you need your site to validate.

The following function created by TheBinaryPenguin for his wp-raw plugin takes care of that by disabling WordPress’s auto-formating filters so that the column shortcode is parsed without being run through them, then reapplying the content filters after the column shortcode has been parsed. The result is that all of the column shortcodes will validate.

function webtreats_formatter($content) {
	$new_content = '';

	/* Matches the contents and the open and closing tags */
	$pattern_full = '{(\[raw\].*?\[/raw\])}is';

	/* Matches just the contents */
	$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';

	/* Divide content into pieces */
	$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

	/* Loop over pieces */
	foreach ($pieces as $piece) {
		/* Look for presence of the shortcode */
		if (preg_match($pattern_contents, $piece, $matches)) {

			/* Append to content (no formatting) */
			$new_content .= $matches[1];
		} else {

			/* Format and append to content */
			$new_content .= wptexturize(wpautop($piece));
		}
	}

	return $new_content;
}

// Remove the 2 main auto-formatters
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

// Before displaying for viewing, apply this function
add_filter('the_content', 'webtreats_formatter', 99);
add_filter('widget_text', 'webtreats_formatter', 99);

Step #4 – Fixing the backtrack_limit Bug

The following takes care of the mysterious disappearing content bug that happens when too many shortcodes are used on a page.

//Long posts should require a higher limit, see http://core.trac.wordpress.org/ticket/8553
@ini_set('pcre.backtrack_limit', 500000);

Step #5 – Converting it all to a Plugin (Optional)

If you don’t want to touch your theme files, you can alternately create a plugin following the steps below.
Or download the finished plugin here: Download the Column Shortcode Plugin Here

1. Create the following folder structure:

Column Shortcodes Plugin Structure1 Adding Column Layout Shortcodes To Your Wordpress Theme

2. Add the following to the top of webtreats-column-shortcodes/column-shortcodes.php so that Wordpress recognizes it as a plugin.

/*
Plugin Name: Fluid Column Layout Shortcodes
Plugin URI: http://tutorials.mysitemyway.com
Description: Adds percentage based infinitely nest-able column shortcodes.
Version: 1.0
Author: Webtreats
Author URI: http://mysitemyway.com
License: GPL2
*/

3. Beneath the plugin info, add all the php code from steps #1,#3,#4.
4. Also add the following additional function to load the necessary CSS styles:

function webtreats_column_stylesheet() {
    $my_style_url = WP_PLUGIN_URL . '/webtreats-column-shortcodes/styles.css';
    $my_style_file = WP_PLUGIN_DIR . '/webtreats-column-shortcodes/styles.css';

    if ( file_exists($my_style_file) ) {
        wp_register_style('column-styles', $my_style_url);
        wp_enqueue_style('column-styles');
    }
}
add_action('wp_print_styles', 'webtreats_column_stylesheet');

5. Add all the css code from step #2 to webtreats-column-shortcodes/styles.css

Step #6 – How to use the shortcodes

You can see the column shortcodes in use on our Awake Wordpress Theme demo here:

http://themes.mysitemyway.com/awake/shortcodes/column-shortcodes/

For pre-formatted starter content in different column configurations just click “Get the Code” beneath each column set and copy/paste the code into your WYSIWYG editor.

We’ve provided a few snippets below as well.

Two column layout:

[one_half]
	Your content goes here.....
[/one_half]
[one_half_last]
	Your content goes here.....
[/one_half_last]

Three column layout:

[one_third]
	Your content goes here.....
[/one_third]
[one_third]
	Your content goes here.....
[/one_third]
[one_third_last]
	Your content goes here.....
[/one_third_last]

Four column layout:

[one_fourth]
	Your content goes here.....
[/one_fourth]
[one_fourth]
	Your content goes here.....
[/one_fourth]
[one_fourth]
	Your content goes here.....
[/one_fourth]
[one_fourth_last]
	Your content goes here.....
[/one_fourth_last]

Five column layout:

[one_fifth]
	Your content goes here.....
[/one_fifth]
[one_fifth]
	Your content goes here.....
[/one_fifth]
[one_fifth]
	Your content goes here.....
[/one_fifth]
[one_fifth]
	Your content goes here.....
[/one_fifth]
[one_fifth_last]
	Your content goes here.....
[/one_fifth_last]

Six column layout:

[one_sixth]
	Your content goes here.....
[/one_sixth]
[one_sixth]
	Your content goes here.....
[/one_sixth]
[one_sixth]
	Your content goes here.....
[/one_sixth]
[one_sixth]
	Your content goes here.....
[/one_sixth]
[one_sixth]
	Your content goes here.....
[/one_sixth]
[one_sixth_last]
	Your content goes here.....
[/one_sixth_last]
Like this Tutorial? Somebody else might too. Consider sharing it on one of these social bookmarking sites.
  • Digg
  • del.icio.us
  • Technorati
  • Design Bump
  • Facebook
  • Reddit
  • StumbleUpon
  • Twitter

Tags: ,

72 Responses to “Adding Column Layout Shortcodes to Your Wordpress Theme”

  1. Dean

    20. Oct, 2010

    Excellent tutorial.
    Perhaps the next could be how to make this work in the WYSIWYG editor as you have with Awake theme

    Reply to this comment
    • Pamela

      21. Oct, 2010

      Hi Dean,
      Glad you like the tutorial!
      Right now our plan is to do individual tutorials for each of our Awake shortcodes. Then we’ll do a tutorial at the very end explaining how to organize them together into one plugin and add the WYSIWYG popup.

      Reply to this comment
      • Matt

        28. Jun, 2011

        I would LOVE a tutorial on how to group multiple shortcodes into a popup in the TinyMCE editor.

        Reply to this comment
  2. Paul

    29. Oct, 2010

    Thanks very much for this tutorial. Very useful.

    Reply to this comment
  3. wparena

    04. Nov, 2010

    I knew how to make column but your tutorial have detail …well explained ..:)

    Reply to this comment
  4. Drew McManus

    06. Nov, 2010

    This is fantastic guys, it works perfectly in my custom theme!

    Reply to this comment
  5. Drew McManus

    06. Nov, 2010

    Any plans on making additional plugins/tuts for the jQuery, style, and tooltip functionality?

    Reply to this comment
    • Pamela

      06. Nov, 2010

      Hi Drew,

      Yes, we do plan on creating tutorials and standalone plugins for each of our shortcodes.

      Reply to this comment
      • Drew McManus

        06. Nov, 2010

        Fantastic, looking forward to it. If you have any sort of time line, I’d love to know about it.

        Reply to this comment
        • Pamela

          10. Nov, 2010

          Unfortunately we don’t have a time frame at the moment, but we will announce it on the support forum when it’s out.

          Reply to this comment
  6. Illusiv

    10. Nov, 2010

    Awesome Tutorial. Thanks for that!

    Reply to this comment
  7. ady

    18. Nov, 2010

    hi
    i tired the plugin it seems not working on front-end i just see shortcode

    Reply to this comment
  8. ady

    18. Nov, 2010

    buttons plugin working fine rendering output please help with column plugin do i have to make changes on my theme side

    Reply to this comment
  9. Wam3

    07. Dec, 2010

    For some reason it works when I preview a post, but when I publish it I get a 404 error? Have you seen that before?

    W

    Reply to this comment
  10. Lalo Marquez

    15. Dec, 2010

    Great!! Thank you for including the installable plugin, you’re the man!!

    Reply to this comment
  11. Kwame

    25. Dec, 2010

    Thanks so much for this, the code has been really useful \!

    Reply to this comment
  12. Adrian

    31. Dec, 2010

    Hi Pamela,

    i want to ask about this column. Can I place it on the index.php? I mean, I want to put this code not on post but on homepage file.

    Reply to this comment
  13. Penina

    18. Jan, 2011

    I installed this plugin and your buttons shortcodes plugin, and both of them deactivated my Slideshow Gallery plugin. My gallery works by inserting a shortcode on a page or post. The gallery plugin I use can be found at the wordpress.org site:
    http://wordpress.org/extend/plugins/slideshow-gallery/screenshots/

    Just wanted to let you know. There’s another free columns plugin, like yours (WP Post Columns), and I’m using that one now instead, even though it hasn’t been updated since late 2010 it still works just fine. The good thing is that it doesn’t disable my slideshow gallery when I install it. I hope you guys work out the kinks because your shortcodes plugins are cool. I’ll try them again if they’re fixed.

    Reply to this comment
  14. Vladimir

    20. Jan, 2011

    Very useful!!! Thank you very much! works great!

    Reply to this comment
  15. empfx

    23. Jan, 2011

    Hi, thanks for taking the time out to share such great code. My problem is when I use the one_fourth shortcode the columns are all vertical and not lining up next to each other, is there anything in my CSS code I need to add or delete to make these line up properly? Thanks again, mega appreciated!

    Reply to this comment
    • empfx

      23. Jan, 2011

      My apologies I have figured out the solution to my problem and thought I would share. I am knew to wordpress so just entering the shortcode itself was’nt enough, each column needed to be in a seperate div and the last column code needed the word “last” appended to it. Hope this helps someone, thanks again for the great shortcodes.

      Reply to this comment
      • Lita

        24. Jan, 2011

        Hi empfx, Glad to hear that the problem is fixed and thank you for sharing the solution!

        Reply to this comment
  16. Brett Widmann

    07. Feb, 2011

    This is a great tutorial! I love the final outcome. Thanks.

    Reply to this comment
  17. Ed

    19. Feb, 2011

    Good stuff! Will try this out soon… and the plugin version.

    Reply to this comment
  18. Saintdo

    01. Mar, 2011

    Cool! I works very well!
    Thanks so much! :D

    Reply to this comment
  19. Patrick

    02. Mar, 2011

    This tutorial is awesome! Added column shortcodes (something I’ve wanted to do for a while) in minutes!

    Im going to check out some of your other posts, keep up the good work!

    Reply to this comment
  20. Newby

    17. Mar, 2011

    Thanks for the detailed description.

    I have just one question Step 1,3,4 code all go into the functions.php?

    Reply to this comment
  21. Jean-Pierre

    23. Mar, 2011

    How would I incorporate that “back to top” style divider? I can style it but getting it into the shortcode plugin would be a great addition

    Reply to this comment
  22. Brian

    29. Mar, 2011

    I can’t tell you how much time this tutorial saved me. Very well-written and organized. This is premium nettuts+ tutorial material :)

    One question – if I use this code in a theme I’m creating for resale on ThemeForest, am I required to give you attribution? If so – where do I put the attribution?

    Thanks again!
    Brian

    Reply to this comment
    • Brian

      29. Mar, 2011

      Haha, wow. I I had scrolled down 3 more inches I would have seen your Terms of Use blurb which totally answers my question. Thanks again!

      Reply to this comment
  23. Natalie

    01. Jun, 2011

    Thanks for posting this code and tutorial – I’d love to be able to use it, but I am getting stymied at Step 1. When I paste the code into functions.php and try to update the file, I get the following error:

    Warning: Cannot modify header information – headers already sent by (output started at /home1/xxx/public_html/websitename/wp-content/themes/themename/functions.php:9) in /home1/xxx/public_html/websitename/wp-admin/theme-editor.php on line 99

    Does this mean anything to you? I’m not advanced enough to figure it out…

    Reply to this comment
  24. Tim

    24. Jun, 2011

    Dear Sir,

    Tim here.
    If your not using it as a plugin how does the shortcodes.php file know what css files to call?

    And also, how can you call the shortcodes.php file from another functions.php page.

    Thanks for your time, great article btw.
    Tim

    Reply to this comment
  25. FashionWriter

    11. Aug, 2011

    I was very happy to come across this tutorial. The designer of the theme I’m using had the width for their columns in pixels which would make the last one appear funky at times. I wouldn’t have been able to figure out the percentages, so this was perfect. And now they work perfectly.

    I added other shortcodes you have here to the few existing ones I had in the shortcodes php file for my theme. All of them work except for the three_fourths one. I’ve double checked the php and the css. It’s right, but it just won’t be called. I don’t understand what could be wrong. I don’t necessarily need it – I would be fine with just the one_fourth – but it’s driving me nuts that all the others are being called fine and not that one. Any ideas?

    Reply to this comment
  26. Pixelrage

    20. Aug, 2011

    Is this article still valid in Aug. 2011? I’m really interested in this but am a little afraid after reading some of these comments!

    Reply to this comment
  27. Lilliana Crevier

    24. Aug, 2011

    Look forward to reading more from you in the future,keep up the good work.

    Reply to this comment
  28. AJ

    30. Aug, 2011

    The CSS was very helpful, thanks!

    Reply to this comment
  29. Pasquale

    07. Sep, 2011

    I just installed the plugin on a wordpress site I’m building. It seems to be doing something funny on the video page when used like this:

    [one_half]
    Solo Work
    [youtubegallery]
    http://youtu.be/aqkX0xf9Uw4
    [/youtubegallery]
    [/one_half]

    The [youtube] is part of another plugin. Is it possible to nest another plugin function within yours? For now, it’s spreading it out into three thumbnails as if [youtubegallery] and [/youtubegallery] are their own clips.

    Reply to this comment
  30. Ash Robbins

    30. Sep, 2011

    Thanks for this tutorial, it’s exactly what I needed and works perfectly!

    Reply to this comment
  31. Penina

    01. Oct, 2011

    The columns shortcode plugin is fantastic! Thank you for the tutorial.

    Reply to this comment
  32. tristar1983

    18. Oct, 2011

    Just what I’m looking for, you’ve save me a bit of time creating this plugin. Thanks again :)

    Reply to this comment
  33. William

    21. Oct, 2011

    Been trying to follow & get this to work on multiple blogs but havent had any success…the plugin works great (thank you!) but hard-coding is not…

    Tried on both a custom theme running 3.2.1, and also a plain old, right out of the box vanilla install….

    is something in the new versions not allowing this? Thanks!

    Reply to this comment
  34. I wonder if I can create a static website with wordpress? Does anybody know where I can find a detailed video tutorial on how to create it from beginning to the end ?

    Reply to this comment
  35. tigroocom

    20. Nov, 2011

    Hi, great post – really useful – thanks. Noticed that that css for four fifths should be a bit bwider, i.e. .four_fifth{ width:79.2%; }

    Reply to this comment
  36. Mike

    29. Nov, 2011

    GREAT!!!!

    thanx for the excellent tutorial. Works for me.

    Reply to this comment
  37. Sascha

    30. Nov, 2011

    Hello There, nice one! I love your tutorial the code works fine form me.
    I have a problem with one of my other plugins i use. I have installed a plugin called digg digg which is adding a floating social button box right beside my content, Now if i activate your columns plugin the box is gone.
    http://www.diggdigg2u.com/

    I am not that familiar with php. So i would love to get a bit of help.
    I have allready emailed the digg digg plugin guy but i he did not email me back.

    Do you think you can help me with that ?

    Sascha

    Reply to this comment
  38. Julie

    09. Dec, 2011

    Hi,

    I love your shortcodes! I purchased both of your themes and just installed this plugin on a third website I’m currently working on.
    The website also uses SimplePress forum plugin. When I activate your plugin it adds unwanted paragraphs everywhere within the forum.
    Any idea why?
    Thanks in advance for your help!
    Julie

    Reply to this comment
  39. Christy

    19. Dec, 2011

    Great tutorial! And, great plugin for Wordpress :)

    Is there a way to modify, or add to the css to allow for a border between the columns?

    I’d like to add a visual separator if possible – it would be great to have it for all, but I’m currently using the 2 column version

    Reply to this comment
  40. fractalbit

    29. Dec, 2011

    Thanks for the great and useful tutorial. I have a question though.

    I do not understand the need for the “webtreats_formatter” function. I assume It only works if you wrap your shortcodes inside a [raw][/raw] shortcode but nowhere in the above code does this happen.

    So what is the purpose of this? Is it optional and left to the user to decide when and how to use it?

    Reply to this comment
  41. Lynn

    31. May, 2012

    Thank you so much for this wonderful solution that’s so elegant, works without any problems, and is easy to implement and use. I’m so happy!!

    Reply to this comment
  42. hamid

    05. Jun, 2012

    Cool! I works very well!

    Reply to this comment
  43. DJIO

    14. Jun, 2012

    Thanks a bunch!
    Awesome material!!
    =)

    Reply to this comment
  44. Trackbacks

    22. May, 2013

    Reply to this comment

Leave a Reply