How to use Widget as Shortcode in WordPress Page, Post

function widget($atts) {
    
    global $wp_widget_factory;
    
    extract(shortcode_atts(array(
        'widget_name' => FALSE,
		'instance' => ''
    ), $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 '

'.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),''.$class.'').'

'; 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'); // To use widget as a Shortcode in Page, Post as [widget widget_name="null_instagram_widget" instance="username=instagranusername&number;=6"] // widget_name - Name of Widget, Go to widget registered code and find name of the Widget // Pass other setting parameter through instance with combine querystring

Leave a Comment

Scroll to Top