[wp-trac] [WordPress Trac] #61792: PHP warning `Undefined array key "widget_id"` in legacy widget
WordPress Trac
noreply at wordpress.org
Wed Jul 31 05:15:47 UTC 2024
#61792: PHP warning `Undefined array key "widget_id"` in legacy widget
--------------------------+-----------------------------
Reporter: rabmalin | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Widgets | Version:
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
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' );
}
);
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/61792>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list