Popular Categories

How to call a WordPress Widget using shortcode?

I recently had this situation where I needed a shortcode for the widget so I can easily place it within the content.

I wanted to just put in the content of the page and have that widget pop in. Turns out it wasn’t as easy I wanted it to be, but it’s not that bad. The answer was creating a custom function for the functions.php file which would output any widget by name.

Here is the snippet of code that can help create a shortcode for a widget.

function widget($atts) {
global $wp_widget_factory;
'widget_name' => FALSE
), $atts));
$widget_name = wp_specialchars($widget_name);
if (!is_a($wp_widget_factory->widgets[$widget_name], 'WP_Widget')):
$wp_class = 'WP_Widget_'.ucwords(strtolower($class));
if (!is_a($wp_widget_factory->widgets[$wp_class], 'WP_Widget')):
return '<p>'.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),'<strong>'.$class.'</strong>').'</p>';
$class = $wp_class;
the_widget($widget_name, $instance, array('widget_id'=>'arbitrary-instance-'.$id,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
$output = ob_get_contents();
return $output;

Now in Post/Page content, you can use the widget by referencing it by name:

[ widget widget_name="Your_Custom_Widget" ]

In case you don’t want to implement the above method, you can use a plugin as an alternative called “Widget Shortcode“.

Was this helpful? Did I miss something? Do you have a question? Get in touch.