Make WordPress Core

Opened 7 weeks ago

Closed 6 weeks ago

#61792 closed defect (bug) (wontfix)

PHP warning `Undefined array key "widget_id"` in legacy widget

Reported by: rabmalin's profile rabmalin Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Widgets Keywords: has-testing-info
Focuses: Cc:

Description

Related to https://core.trac.wordpress.org/ticket/53548

In the ticket, it is mentioned that this issue has been fixed but I am still getting PHP notice in WP 6.6.1

Log:

PHP Warning:  Undefined array key "widget_id" in /Users/****/plugins/my-widget-plugin/my-widget-plugin.php on line 29

This appears in log file whenever I go to Appearance -> Widgets in the admin panel with following plugin activated.

This is the plugin code:

<?php
/*
Plugin Name: My Widget Plugin
Description: A simple plugin to create a custom widget.
Version: 1.0.0
Author: John Doe
*/

/**
 * Widget class.
 */
class My_Custom_Widget extends WP_Widget {
	public function __construct() {
		parent::__construct(
			'my_custom_widget',
			__( 'My Custom Widget', 'my-widget-plugin' ),
			[ 'description' => __( 'A Custom Widget', 'my-widget-plugin' ) ]
		);
	}

	// Front-end display of the widget
	public function widget( $args, $instance ) {
		echo $args['before_widget'];

		if ( ! empty( $instance['title'] ) ) {
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
		}

		var_dump( $args['widget_id'] );

		echo $args['after_widget'];
	}

	public function form( $instance ) {
		$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'my-widget-plugin' );
		?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'my-widget-plugin' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
			</p>
		<?php
	}

	public function update( $new_instance, $old_instance ) {
		$instance = [];

		$instance['title'] = ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '';

		return $instance;
	}
}

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

Attachments (1)

widget.png (556.5 KB) - added by mukesh27 7 weeks ago.

Download all attachments as: .zip

Change History (5)

#1 @narenin
7 weeks ago

  • Keywords has-testing-info added

Test Report

I have tried to replicate this issue and able to replicate the same.


Environment

WordPress: 6.6.1
PHP: 8.1.23
Server: nginx/1.16.0
Database: mysqli (Server: 8.0.16 / Client: mysqlnd 8.1.23)
Browser: Chrome 126.0.0.0 (macOS)
Theme: Twenty Sixteen (Version: 3.3)

Results:

I am also getting the error in the error log.

2024/07/31 11:05:00 [error] 971#971: *2 FastCGI sent in stderr: "PHP message: PHP Warning:  Undefined array key "widget_id" in /Users/****/plugins/my-widget-plugin/my-widget-plugin.php on line 29


@mukesh27
7 weeks ago

#2 @mukesh27
7 weeks ago

Don't get error. Did i miss anything?

#3 @narenin
7 weeks ago

Hi @mukesh27

This error will appear in log file whenever we go to Appearance -> Widgets in the admin panel and plugin was activated.

You can then check the error log file for the same.

#4 @peterwilsoncc
6 weeks ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

The widget method in plugins doesn't always receive an widget_id and need to check if it's defined before use. See the recent posts widget's code as an example.

Based on a quick scan of the code it's due to the widgets not having an instance ID when displayed outside of a sidebar on the widgets page.

As the error is being triggered by a plugin, I've closed this ticket as I don't think there is much core can do to defend against the situation.

Note: See TracTickets for help on using tickets.