Skip to Content
Menu

Widget

This shortcode allows you to call any widget by name along with it’s parameters (“instances”).

WordPress August 19, 2017

Usage

WordPress
[widget widget_name instances]

Parameters

widget_name
(Required) (String) The name of the widget to call
Default: None

instances
(Optional) (String) Attributes for the widget
Default: None

Parameter Notes

The instances parameter is actually a series of attribute=”value” parameters. The attribute should be the parameter name from the widget. See examples for more help.

Request or provide clarification »

Examples

Call a widget without using instances

WordPress
[widget widget_name="nebula_login_form"]

Call a widget with custom settings

WordPress
[widget widget_name="nebula_linked_image" image="http://placehold.it/300x300" url="http://google.com"]

Call this function from a PHP template

PHP
<?php echo do_shortcode('[widget widget_name="nebula_linked_image" image="http://placehold.it/300x300" url="http://google.com"]'); ?>

Call a widget directly via PHP without this function

PHP
<?php the_widget('nebula_linked_image', array('image' => 'http://placehold.it/300x300', 'url' => 'http://google.com')); ?>
Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Shortcodes.php on line 112.

    WordPress
            public function widget($atts){
                global $wp_widget_factory;
    
                //Get widget fields
                $instance = array();
                foreach ( $atts as $attribute => $value ){
                    if ( $attribute !== 'widget_name' ){
                        $instance[$attribute] = $value;
                    }
                }
    
                extract(shortcode_atts(array(
                    'widget_name' => false,
                ), $atts));
    
                ob_start();
    
                //Call the widget directly via PHP: https://codex.wordpress.org/Template_Tags/the_widget
                the_widget(esc_html($widget_name), $instance, array(
                    'widget_id' => 'arbitrary-instance-' . random_int(100000, 999999), //PHP 7.4 use numeric separators here
                    'before_widget' => '',
                    'after_widget' => '',
                    'before_title' => '',
                    'after_title' => ''
                ));
    
                $output = ob_get_contents();
                ob_end_clean();
    
                return $output;
            }
    

    Override

    To override or disable this shortcode, use add_shortcode() to declare the shortcode first.