1

I'm trying to add two widgets to a Wordpress plugin I'm developing. What is the correct way to call multiple classes?

I have a plugin folder containing my-plugin.php:

class WidgetA extends WP_Widget {
  function widget_a() {
    ...
  }
  function form($instance) {
    ...
  }
  function update($new_instance, $old_instance) {
    ...
  }
  function widget($args, $instance) {
    ...
  } 
}
add_action('widgets_init', create_function('', 'return register_widget("WidgetA");'));

class WidgetB extends WP_Widget {
  function widget_b() {
    ...
  }
  function form($instance) {
    ...
  }
  function update($new_instance, $old_instance) {
    ...
  }
  function widget($args, $instance) {
    ...
  } 
}
add_action('widgets_init', create_function('', 'return register_widget("WidgetB");'));

If I delete WidgetA then WidgetB functions correctly, and vice versa. But nothing is displayed if I include both classes.

What is the correct way to define two widgets/ classes in a plugin?

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
  • Possible duplicate of [How to get useful error messages in PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – CBroe Jan 09 '16 at 03:10

1 Answers1

3

You can declare multiple widget classes into single plugin using below code :

add_action( 'widgets_init', 'src_load_widgets' );

function src_load_widgets() {
    register_widget( 'WidgetA' );
    register_widget( 'WidgetB' );
}

class WidgetA extends WP_Widget {
    // ...
}

class WidgetB extends WP_Widget {
    // ...
}

Source : https://wordpress.stackexchange.com/questions/47492/wordpress-multiple-widget-in-single-plugin

Community
  • 1
  • 1
Milap
  • 6,915
  • 8
  • 26
  • 46