2

I am having two widgets. They are created in the same file-widget.php . If i keep one widget only then it will work without any issues. But when they are together one will stop working(it will not be dragable).

My code is below.

class Widget_1 extends WP_Widget {

    //setup the widget name, description, etc...
    public function __construct() {

        $widget_ops = array(
            'classname' => 'widget1',
            'description' => 'Description',
        );
        parent::__construct( 'widget1', 'Widget 1', $widget_ops );

    }

    //back-end display of widget
    public function form( $instance ) {
        echo '<p><strong>Some text here</p>';
    }

    //front-end display of widget
    public function widget( $args, $instance ) {

        echo $args['before_widget'];

    //there are fields in between this

        echo $args['after_widget'];
    }

}
add_action( 'widgets_init', function() {
    register_widget( 'Widget_1' );
} );




class Widget_2 extends WP_Widget {

    //setup the widget name, description, etc...
    public function __construct() {

        $widget_ops = array(
            'classname' => 'widget2',
            'description' => 'Description',
        );
        parent::__construct( 'widget2', 'Widget 2', $widget_ops );

    }

    //back-end display of widget
    public function form( $instance ) {
        echo '<p><strong>Sometext here</p>';
    }

    //front-end display of widget
    public function widget( $args, $instance ) {

        echo $args['before_widget'];

  //there are fields in between this

        echo $args['after_widget'];
    }

}
add_action( 'widgets_init', function() {
    register_widget( 'Widget_2' );
} );


When i change the

public function form( $instance )

to

public function forms( $instance )

Both starts to work but save button wont show up for one plugin.

Thanks

6
+50

Reason is pretty simple and easy to overlook... It has nothing to do with placing both widgets in the same file or anything like this...

Let's look at your code that is responsible for printing the widgets' form:

public function form( $instance ) {
    echo '<p><strong>Sometext here</p>';
}

Do you see it now? There are unclosed tags in there...

You open <p> tag, then open <strong> tag and never close it. This causes incorrect JS behavior - so you can't drag&drop second widget...

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.