PHP Warning: Undefined array key “post_type” in class-backend.php
-
Environment:
- WordPress 7.1
- PHP 8.3.33
- WooCommerce 11.0.1
- Server: Apache
Description:
Getting the following warning in the logs:
PHP Warning: Undefined array key "post_type" in [site]/wp-content/plugins/search-exclude/lib/controllers/class-backend.php on line 173Cause:
Line 173 reads
$_REQUEST['post_type']directly without checking whether the key exists first:php
public function save_post_ids_to_search_exclude( $post_ids, $exclude ) { $post_type = $_REQUEST['post_type'];On PHP 8.0+, accessing an undefined array key raises a warning instead of silently returning
NULL(as it did on PHP 7.x). Sincesave_post_ids_to_search_exclude()is presumably being called (directly or via a hook) in a request that doesn’t include apost_typeparameter, this fires every time.Suggested fix:
Use
isset()with the null coalescing operator so it degrades gracefully whenpost_typeisn’t present:php
$post_type = isset( $_REQUEST['post_type'] ) ? sanitize_key( $_REQUEST['post_type'] ) : '';(Adding
sanitize_key()here too, since this is unsanitized$_REQUESTinput being used as an array key shortly after.)Impact:
Cosmetic/log-noise only in my case so far — doesn’t appear to break functionality — but it’s worth fixing since PHP is trending toward stricter handling of undefined array access in future versions, and it clutters error logs on sites with logging/monitoring enabled.
You must be logged in to reply to this topic.