| |

Include a Sidebar with a Shortcode in WordPress

Recently I was faced with a pretty interesting problem where I wanted to have a page that was structured: Content – Widgets – Content. Instead of hacking together some sort of Content – Sidebar – Metadata on the backend, I decided to make sidebar widgets accessible via a shortcode you can use in the WordPress editor. You can download the code on Github here.

The crux of the matter is three function focused around the output buffer: ob_start(), ob_get_contents(), and ob_end_clean(). ob_start() will tell PHP to place any following output into a buffer that you can access via ob_get_contents(). So for example, in this code:

ob_start();
 print "Hello World!";
 $text= ob_get_contents();

The value of $text would be Hello World. You would end the output buffer with ob_end_clean(). With that in mind, let’s look at the shortcode!

If you need a primer on WordPress shortcodes, you can start with the WordPress Codex’s entry (you can also pick up either of my WordPress books, wink wink nudge nudge). Our shortcode will look like this: [get_sidebar]. It can also accept one argument for name (really slug) of the sidebar. So if you have a sidebar named “home” you can do [get_sidebar name=home]. Here’s our function:

function sidebar_shortcode($atts, $content="null"){
  extract(shortcode_atts(array('name' => ''), $atts));

  ob_start();
  get_sidebar($name);
  $sidebar= ob_get_contents();
  ob_end_clean();

  return $sidebar;
}

By default, the argument $name will be set to blank, so the default sidebar would be called. Notice out ob_ functions; after ob_start(), we call get_sidebar(), which means the entire output from that function will be put on the buffer, which we then throw into the $sidebar variable and return. It’s very important to called ob_end_clean()! If you don’t, the sky will fall on your head.

The last step is to register the short code:

add_shortcode('get_sidebar', 'sidebar_shortcode');

And that’s it! You’re done. It’s worth nothing that you should prefix both your function and your shortcode to avoid conflicts. Again, you can download the code from Github.

Similar Posts

  • | |

    JLC Web Design

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! Over the last 6 months I have been designing a redesigning the website for my web design company, JLC Web…

  • New way to comment!

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Dude to the massive amounts of spam that I just got today (over ~50 messages in a few hours), I installed…

  • |

    Coding Process

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Here is a paper I recently found. It’s on my coding process (after being a coder for about 2.5 months), and…

  • | |

    Freelancing the Net

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! I am very excited to announce that yesterday I launched a new blog, called Freelancing the Net. It’s a blog…

  • Automating Saved Links with Pocket And Zapier

    Back before I decided to start automating more, the way I would compile my newsletters was to save links to Pocket and then on Monday mornings, scour them for what to share, create the links, and sum up the stories. I also had to be careful that I didn’t share the same link two weeks…

  • | |

    My Everyday Things, Part 1: Workflow

    Recently I started following Everyday Carry, a blog dedicated to showcasing the items that people must have on an everyday basis. I decided that in an effort to blog more, I would do a short, 3 part series on the stuff I use everyday. The series will be broken up into 3 parts: today’s installment…

One Comment

Comments are closed.