Title: Sidebar shortcode
Last modified: August 21, 2016

---

# Sidebar shortcode

 *  Resolved [melbo](https://wordpress.org/support/users/melbo/)
 * (@melbo)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/sidebar-shortcode/)
 * Hello,
 * I am trying to create a shortcode for the wordpress default sidebar so it can
   be added everywhere in the page.
 * So i created the sidebar:
 *     ```
       // Sidebar Shortcode
   
         function shortcode_sidebar( $atts, $content = null ) {
           extract(shortcode_atts(array(
             'type' => 'vertical'
           ), $atts));
   
           if ($type == 'horizontal') { include(TEMPLATEPATH."/footer_sidebar.php"); }
           else { get_sidebar(); } 
   
         }
       ```
   
 * And when I add it in the editor, the sidebar appears as it was projected to appear
   just that if I place it in a <div> container
 * `<div class="sidebar-container">[sidebar]</div>`
 * The sidebar is taken out of the div container and is listed above.
 * Do you have any explanation for that?
 * Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/sidebar-shortcode/#post-4090331)
 * anything in a shortcode needs to be **return**ed instead of printed/**echo**ed–
   however, the call of `get_sidebar()` calls `dynamic_sidebar()` which directly
   outputs the widgets.
 * you could try to work with output buffering; [http://php.net/manual/en/book.outcontrol.php](http://php.net/manual/en/book.outcontrol.php)
 *  Thread Starter [melbo](https://wordpress.org/support/users/melbo/)
 * (@melbo)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/sidebar-shortcode/#post-4090335)
 * With alchymyth’s help I managed to rewrite the code as follows:
 *     ```
       // Sidebar Shortcode
   
         function shortcode_sidebar( $atts, $content = null ) {
           extract(shortcode_atts(array(
             'type' => 'vertical'
           ), $atts));
   
           if ($type == 'horizontal') {
       	    ob_start();
       	    include(TEMPLATEPATH."/footer_sidebar.php");
       	    $string_to_return = ob_get_contents();
       	    ob_end_clean();
       	    return $string_to_return;
           }
           else {
           	ob_start();
       	    get_sidebar();
       	    $string_to_return = ob_get_contents();
       	    ob_end_clean();
       	    return $string_to_return;
           	 } 
   
         }
   
         add_shortcode('sidebar', 'shortcode_sidebar');
       ```
   
 * And it works.
 * Thanks a lot!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Sidebar shortcode’ is closed to new replies.

## Tags

 * [shorcode](https://wordpress.org/support/topic-tag/shorcode/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [melbo](https://wordpress.org/support/users/melbo/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/sidebar-shortcode/#post-4090335)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
