| |

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

  • |

    5 Steps to Giving a Great Conference Talk

    When Steve Jobs presented the iPhone for the first time, he didn’t get up on stage and say, “Hey this is an iPhone.” Instead, he told a story – specifically the story of Apple. He built up the iPhone in terms that people understood. This made for an excellent presentation. It sucked people in, it…

  • Word Pres 1.5

    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 just upgraded! YAY. You will probably be looking at this layout for a couple of days which i decide if…

  • Never Assume When Teaching WordPress

    Over the weekend I gave a Lightning Talk at WordCamp US called Never Assume When Teaching WordPress. The response was overwhelmingly great! While I don’t normally do this, since it was a shorter talk I’ve decided to post the script I wrote for it. You can check it out after the jump.

  • | |

    Fading Pages in Javascript

    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 not a huge proponent of Javascript. However, I recognize that it is becoming more and more powerful and used,…

  • |

    Fix wp_nav_menu on Custom Type Archives in WordPress

    Recently I was working with an issue in WordPress where the site’s menu was not showing up on an archive page for one of my custom post types. There were a few troubleshooting things I tried, including the most common recommendation, use theme_location instead of menu when referencing the menu in your theme (code after…

One Comment

Comments are closed.