Opened 4 years ago
Closed 4 years 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)
Change History (14)
#3
@
4 years ago
This looks very similar too https://github.com/WordPress/gutenberg/issues/33056
#5
@
4 years 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
@
4 years 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.
#8
@
4 years ago
There is now a PR on the gutenberg plugin. https://github.com/WordPress/gutenberg/pull/33152
This ticket was mentioned in PR #1459 on WordPress/wordpress-develop by spacedmonkey.
4 years ago
#10
Trac ticket: https://core.trac.wordpress.org/ticket/53548
#11
@
4 years 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.
Hi @subrataemfluence,
Could you please retest using the beta4 release to see if this was resolved? Beta3 should be considered outdated at this point.