1

Has someone ever tried to extend calss WP_Widget, then extend it again ?

Having to related widgets, I tried to create the first extending WP_Widget, :

class FirstWidget  extends WP_Widget {
  public function __construct(){
    parent::__construct(

etc

and then extending the First to create the second one :

class SecondWidget  extends FirstWidget  {
  public function __construct(){
    parent::__construct(

this works well, except that the second widget isn't available in the drag and drop widgets area. I can explicitly use it in my code :

the_widget('SecondWidget', array('title'=> "New title")

and it shows well, but I can't drag'n'drop it.

Someone knows why ?

3

You have to make sure that all of your parameters are passed all the way through to the WP_Widget class. Your code is very truncated but I am pretty sure that is what you are doing wrong.

The widget code below works-- it doesn't do much of anything, but it works. See if you can use it as a template to sort out your own code.

  class Foo extends WP_Widget {
    /*constructs etc*/
    function __construct($id = 'twidg', $descr = 'Test Widget', $opts = array()) {
      $widget_opts = array();
      parent::__construct($id,$descr,$widget_opts);
      /*do stuff*/
    }

    function widget() {
      echo 'test widget';
    }
  }

function rw_cb() {
  register_widget("Foo");
}
add_action('widgets_init', 'rw_cb');

class Bar extends Foo {     
    function __construct() {
      $widget_opts = array();
      parent::__construct('twidgextended','Test Widget 2',$widget_opts);
    }

    function widget() {
      echo 'test widget 2';
    }
}
function rw_cb_2() {
  register_widget("Bar");
}
add_action('widgets_init', 'rw_cb_2');

The code is mostly cribbed from another question. I made minor changes before posting it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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