Put WordPress Widget Inside Post or Page Via Shortcode

Written by: admin | Date: Sunday, March 27th, 2016
Categories: ReferenceWordPress

Below WordPress function to show a widget via wordpress shortcode. You can use this to put a widget inside of a post, page, or even a widget.

function widget($atts) {
    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'); 

In the post or page insert this shortcode:
[widget widget_name="widget_name"]


WordPress Bootstrap Nav Walker Strict Standards Warning

Written by: admin | Date: Saturday, March 26th, 2016
Categories: WordPress

After turning on WordPress Debug on one of my custom themes I noticed a strict standards waring regarding the Navwalker Bootstrap menu.

Error:

Strict Standards: Declaration of… start_lvl() should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = Array)

Fix:

change

function start_lvl(&$output, $depth) {

to

function start_lvl(&$output, $depth = 0, $args = array()) {