Skip to content

Instantly share code, notes, and snippets.

@cillosis
Last active December 13, 2015 20:58
Show Gist options
  • Save cillosis/4973477 to your computer and use it in GitHub Desktop.
Save cillosis/4973477 to your computer and use it in GitHub Desktop.
Make any WordPress Plugin insertable with a Shortcode
/**
* Any Widget As ShortCode
* @uses http://digwp.com/2010/04/call-widget-with-shortcode/
*/
function widget($atts) {
// Initialize values
$instance = null;
$id = null;
global $wp_widget_factory;
extract(shortcode_atts(array(
'widget_name' => FALSE
), $atts));
$widget_name = esc_html($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>';
else:
$class = $wp_class;
endif;
endif;
ob_start();
the_widget($widget_name, $instance, array('widget_id'=>'arbitrary-instance-'.$id,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
));
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('widget','widget');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment