Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    based on the solution mentioned on https://digwp.com/2010/04/call-widget-with-shortcode/ , you can use the code below.

    function wysiwyg_widget( $atts ) {
    
        global $wp_widget_factory;
    
        extract( shortcode_atts( array(
            'widget_name' => "WYSIWYG_Widgets_Widget",
    		'id' => null,
        ), $atts ) );
    
        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;
    
    	if ( ! is_null( $id ) ) {
    		$instance['wysiwyg-widget-id'] = $id;
    	}
    
        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( 'wysiwyg_widget','wysiwyg_widget' );

    And then you can use either
    [wysiwyg_widget id="<ID>"]
    or even
    <?php echo do_shortcode('[wysiwyg_widget id="<ID>"]'); ?>

    Genius!!
    Thank you.

    Expanded upon EXED’s solution. To decide on showing the title or not.

    function wysiwyg_widget( $atts ) {
    
        global $wp_widget_factory;
    
        extract( shortcode_atts( array(
            'widget_name' => "WYSIWYG_Widgets_Widget",
    		'id' => null,
    		'show_title' => "1",
        ), $atts ) );
    
        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;
    
    	if ( ! is_null( $id ) ) {
    		$instance['wysiwyg-widget-id'] = $id;
    	}
    
    	if ( ! is_null( $show_title ) ) {
    		$instance['show_title'] = $show_title;
    	}
    
        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( 'wysiwyg_widget','wysiwyg_widget' );

    Then change your short to reflect if you want title on or off (Title is on by default). On = “1” Off = “0”
    [wysiwyg_widget id="<ID>" show_title="0"]
    or
    <?php echo do_shortcode('[wysiwyg_widget id="<ID>" show_title="0"]'); ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘php to call the widget anywhere’ is closed to new replies.