Insert a sidebar in page content with shortcode

 * Inserts sidebar in a post or page
 * Use: [get_sidebar name=""]
 * @param      $name      Sidebar Slug
 * @version 1.2
 * @author ENDif Media
 function do_midtext_sidebar($atts, $content = 'null'){
 	extract( shortcode_atts(  array(
        'name' => '',
    ), $atts ) );

     * extract() gives us all of our $atts as individual variables 
     * this is why we can call 'name' as $name. cool!
    if (is_active_sidebar($name)){

        $widgets = ob_get_contents();

        return $widgets;

    } else {

        //else return nothing
        return '';

Recently I was tasked with a problem where I wanted to have a page that was structured: Content – Widgets – Content. Instead of hacking together some sort of content meta box on the backend, I decided to make widgets accessible via a shortcode you can use in the WordPress editor.

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

print "WPSNIPP.IT Rules!";
$text= ob_get_contents();

The value of $text would be WPNIPP.IT Rules!. You would end the output buffer with ob_end_clean().

Our shortcode will look like this: [get_sidebar]. It can also accept one argument for name (slug name) of the sidebar. So if you have a sidebar named “sidebar-1″ you can use [get_sidebar name=”sidebar-1”]. You can use this shortcode multiple times in one page, great for including ads within your content.

Place this code in your child theme’s functions.php file or turn it into a plugin and that’s it!