2

I have my own widget, that uses WP_Widget class, and executes its code inside function in this class. Is there any ways to run this widget using shortcode inside post?

This is how my widget structure looks like:

add_action( 'widgets_init', 'register_my_widget' );
function register_my_widget() {}
class My_Widget extends WP_Widget () {
    function My_Widget() {}
    function widget() {}
    function update() {}
    function form() {}
}

How can I add the shortcode for executing this widget?

1 Answer 1

0

I think you are out of luck as wordpress core doesn't have this kind of functionality. Quick googling shows that there are plugins to do that like this one http://wordpress.org/extend/plugins/amr-shortcode-any-widget/, but if you have programming skills you should probably write a shortcode handler with/instead of the widget.

2
  • Thanks for the answer. I already found this digwp.com/2010/04/call-widget-with-shortcode method, but it's inappropriate for me, because there is manipulation with core files. Also I found wordpress.org/extend/plugins/gtrans this plugin, which can be called with both shortcode and widget methods. I think, it's possible to do this with help of another function in the widget class like this: widget(){calling to sc_handler()}; shortcode_handler(){my code}.
    – drake2300
    Nov 29, 2012 at 12:40
  • yes, you should have the main do_stuff($args) function somewhere in your plugin, and let it handle all the generation of the output based on the parameters, then just call it from your widget code and shortcode code. Just keep it a simple function and not part of the widget class so it will be easier to call it from the shortcode. Nov 30, 2012 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.