Allow ability to pass parameters when registering widgets

  1. Timothy Wood


    I have run into an issue several times when calling register_widget( 'MyNewWidget' ); for a shared widget class it instantiates the widget over any previously called register_widget() calls to the same class. My proposal is to implement a change of the WP_Widget_Factory::register method to allow passing parameters thus allowing unique constructor calls when registering. My proposed change can be seen as an extension of the WP_Widget_Factory now: https://gist.github.com/2437027

    Posted: 6 years ago #
  2. Dieta Dukana

    I don't think many people have ran into the same problem as you Timothy, but I think that this is a good idea and would make wordpress better.

    Posted: 6 years ago #
  3. Wonder


    I do not think this is strictly required, I think wordpress developers will overlook.

    Posted: 6 years ago #
  4. Hello Timothy,
    first of all, excuse me for my bad english, i'm from Germany. I have to say that you problem is not too rare. But have you checked your code at this point?:
    Class Extend_WP_Widget_Factory extends WP_Widget_Factory {

    In the most cases the fault could be found here.

    I hope you can solve your problem!
    with best regards,
    Jens Zalando

    Posted: 6 years ago #
  5. Tien Nguyen

    This way is much better than, add your widget naturally into WordPress stream:

    global $wp_widget_factory;
    $wp_widget_factory->widgets[$id] = new WP_Custom_Widget($args);
    Posted: 3 years ago #
  6. xoogu

    I agree adding the ability to pass constructor parameters would be useful. At the moment you need to use the hack Tien Nguyen used if you want to pass parameters to the widget.

    Another option could be to have a do_action in your widget constructor, to which you pass the widget instance. That would then allow you to register a callback for the action where you could inject the parameters into the object. But that would be even messier than Tien's solution IMO.

    Posted: 3 years ago #
  7. Tazo Todua

    That would be useful. At this moment I have to use a "hack" (unreliable though):

    $GLOBALS['my_global_var'] = 123;

    and then access $GLOBALS['my_global_var'] in your widget class.

    Posted: 4 months ago #
  8. Liviu Serdin

    It's been over six years. It would be very useful if implemented!

    Posted: 3 months ago #

RSS feed for this topic


You must log in to post.

  • Rating

    5 Votes
  • Status

    This idea is under consideration