0
votes

I created a PLUGIN with two WIDGETS. 1st widget goes in the sidebar, but the 2nd widget needs to be displayed in a page post using shortcode [mywidget] How can I make this happen? Basically I need "hello me" to be displayed in the page post using [mywidget] shortcode.

//register widget FUNCTION
add_action('widgets_init', 'ds_register_widgets');

//call register widget FUNCTION
function ds_register_widgets(){
    register_widget('ds_bm_info');
    register_widget('ds_bm_odds');
}

//widget class
class ds_bm_info extends WP_Widget { }

//widget class
class ds_bm_odds extends WP_Widget {

function widget( $args, $instance ) {
        echo "hello me";
    }
}
1

1 Answers

0
votes

For a simple function :

add_shortcode('mywidget', 'mywidget_function');

function mywidget_function() {
   echo "Hello me";
}

If it's inside the widget class :

add_shortcode( 'mywidget', array( 'ds_bm_odds', 'mywidget_function' ) );

class ds_bm_odds extends WP_Widget {

    public function mywidget_function()
    {
        echo "Hello me";
    }
    // ...

}

Both should work. But if you don't use the admin widget management (i.e. pass special paramaters to the shortcode), there's no need to register this shortcode inside a widget.