Title: Use the_widget() inside functions.php
Last modified: August 21, 2016

---

# Use the_widget() inside functions.php

 *  Resolved [tiagofaia](https://wordpress.org/support/users/tiagofaia/)
 * (@tiagofaia)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/use-the_widget-inside-functionsphp/)
 * Hi .
 * I’m trying to create a shortcode with some html and a widget, but the widget 
   is printing outside the divs. I think I can’t use the_widget(), the right way
   would be get_widget(), but doesn’t exists any get_widget()…
 * The widget it’s showing before my divs.
 * My code:
 *     ```
       // Calendar shortcode
   
       function calendarevents_function()
       {
       	$a1 = "<div class='parallax-img content-area'><div class='site-content' role='main'><div class='entry-content'>";
       	$a2= "</div></div></div>";
   
       return ($a1.the_widget('TribeEventsMiniCalendarWidget').$a2);
       }
   
       add_shortcode('calendarevents', 'calendarevents_function');
       ```
   
 * Any suggestions?

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/use-the_widget-inside-functionsphp/#post-4638603)
 * The widget is echoed and it needs to be returned.
    [http://codex.wordpress.org/Shortcode_API#Output](http://codex.wordpress.org/Shortcode_API#Output)
 * Try and use an output buffer.
    [http://nl3.php.net/ob_start](http://nl3.php.net/ob_start)
 *     ```
       function calendarevents_function() {
   
       	$a1 = "<div class='parallax-img content-area'><div class='site-content' role='main'><div class='entry-content'>";
       	$a2= "</div></div></div>";
   
       	ob_start();
       	the_widget( 'TribeEventsMiniCalendarWidget' );
       	$widget = ob_get_contents();
       	ob_end_clean();
   
       	return ($widget) ? $a1 . $widget . $a2 : '';
       }
   
       add_shortcode( 'calendarevents', 'calendarevents_function' );
       ```
   
 *  Thread Starter [tiagofaia](https://wordpress.org/support/users/tiagofaia/)
 * (@tiagofaia)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/use-the_widget-inside-functionsphp/#post-4638608)
 * you saved my life! It works!!
 * Thanks Keesiemeijer!
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/use-the_widget-inside-functionsphp/#post-4638610)
 * You’re welcome. I’m glad you’ve got it resolved 🙂
 *  [anou](https://wordpress.org/support/users/anou/)
 * (@anou)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/use-the_widget-inside-functionsphp/#post-4638887)
 * Big thanks [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)!!!

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

The topic ‘Use the_widget() inside functions.php’ is closed to new replies.

## Tags

 * [the_widget](https://wordpress.org/support/topic-tag/the_widget/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 3 participants
 * Last reply from: [anou](https://wordpress.org/support/users/anou/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/use-the_widget-inside-functionsphp/#post-4638887)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
