WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 3 months ago

#53548 closed defect (bug) (fixed)

Widget Block: widget_id is undefined when a widget is placed

Reported by: subrataemfluence Owned by: desrosj
Milestone: 5.8 Priority: normal
Severity: major Version: 5.8
Component: Widgets Keywords: has-screenshots has-patch
Focuses: administration Cc:

Description

I am not sure whether this is already under discussion.
While playing with the new Widget block of 5.8 Beta 3, I got the following notice:

Undefined index: widget_id

I am only trying to create an element inside the widget() method:

<?php
public function widget( $args, $instance ) { ?>
   <div id="element-$args['widget_id']">...</div>
<?php }

However, when I switch to the classic Widget page more, the error goes away and a proper widget_id is returned.

<?php
public function widget( $args, $instance ) {
   if( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } ?>
   <div id="element-$args['widget_id']">...</div>
<?php }

The undefined notice does go away with the above modification, but $this->id is always -1. In general, an element with ID like element--1, is fine, but the problem starts when multiple instances of the same widget are placed and JavaScript is unable to locate the right element. The ID of each instance becomes the same.

How to grab the ID of the current widget while on a Block based widget page?

Attachments (1)

widget_id.png (28.2 KB) - added by subrataemfluence 3 months ago.

Download all attachments as: .zip

Change History (14)

#1 follow-up: @desrosj
3 months ago

  • Keywords reporter-feedback added

Hi @subrataemfluence,

Could you please retest using the beta4 release to see if this was resolved? Beta3 should be considered outdated at this point.

#2 @desrosj
3 months ago

  • Milestone changed from Awaiting Review to 5.8

#4 @spacedmonkey
3 months ago

Also looks similar too #53452

#5 @desrosj
3 months ago

  • Milestone changed from 5.8 to 5.8.1

Since RC1 will be packaged in the next few minutes, I'm going to punt this to 5.8.1.

#6 in reply to: ↑ 1 @subrataemfluence
3 months ago

As per your suggestion, I updated to 5.8-RC1 and the problem persists.
I am still getting Undefined Index error against $args['widget_id']

I am using a fresh 2021 theme and no additional plugin has been installed except Akismet.

Here is my widget code:

<?php
class Quick_Periodic_Table extends WP_Widget {
   public $id_base = 'quick_periodic_table_id';
   
   public function __construct() {
      $widget_option   = array(
        'id' => $this->widget_id,
        'classname'   => 'quick_periodic_table_class',
        'description' => __( 'Place a periodic table ' ),
        'customize_selective_refresh' => true,
        'show_instance_in_rest'       => true,
      );

      $control_options = array(
        'width'   => '90%',
        'id_base' => $this->id_base,
      );
                
      parent::__construct(
        $this->id_base,
        __( 'Quick Periodic Table', 'qpt' ),
        $widget_option,
        $control_options
      );
   }

   ...
   
   public function widget( $args, $instance ) {
       extract( $args ); ?>
       <div id="<?= $args['widget_id'] ?>">
            // ...
       </div>
   <?php }

   ...
}

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

Replying to desrosj:

Hi @subrataemfluence,

Could you please retest using the beta4 release to see if this was resolved? Beta3 should be considered outdated at this point.

#7 @subrataemfluence
3 months ago

  • Keywords has-screenshots added

#8 @spacedmonkey
3 months ago

There is now a PR on the gutenberg plugin. https://github.com/WordPress/gutenberg/pull/33152

#9 @spacedmonkey
3 months ago

  • Keywords has-patch added

#11 @spacedmonkey
3 months ago

  • Owner set to desrosj
  • Status changed from new to assigned

https://github.com/WordPress/gutenberg/pull/33152 has been merged and marked for backporting.

#12 @desrosj
3 months ago

  • Keywords reporter-feedback removed
  • Milestone changed from 5.8.1 to 5.8

#13 @desrosj
3 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

This should be resolved by [51344]/[51346].

Note: See TracTickets for help on using tickets.