Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#61879 reopened defect (bug)

Deprecation Warning

Reported by: pcalvo Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version: 6.7
Severity: minor Keywords: has-patch
Cc: Focuses:

Description

When accessing the WordPress admin page, a deprecation warning is displayed in the error logs:

WARNING: [pool www] child 7 said into stderr: "NOTICE: PHP message: PHP Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /public/wordpress/wp-includes/formatting.php on line 4720"

This issue arises because the strip_tags() function is being passed a null value. As of PHP 8.2, passing null to strip_tags() is deprecated and will trigger a warning. The code should ensure that a non-null value is passed to strip_tags() to prevent this warning.

Attachments (1)

fix-deprecation-warning.diff​ (501 bytes ) - added by pcalvo 2 years ago.
fix

Download all attachments as: .zip

Change History (7)

This ticket was mentioned in ​PR #7202 on ​WordPress/wordpress-develop by ​Serfe-com.


2 years ago
#1

Fix warning for NOTICE: PHP message: PHP Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /public/wordpress/wp-includes/formatting.php on line 4720

Trac ticket: https://core.trac.wordpress.org/ticket/61879

#2 @pamprn
2 years ago

  • Resolution → worksforme
  • Status new → closed

Good call!

#3 @SergeyBiryukov
2 years ago

  • Resolution worksforme
  • Status closed → reopened

This ticket was mentioned in ​PR #7211 on ​WordPress/wordpress-develop by ​@deepakrohilla.


2 years ago
#4

esc_textarea function is being passed a null value, and the htmlspecialchars() function within it expects a string, not null. This is reason getting the deprecation warning in PHP.

https://core.trac.wordpress.org/ticket/61879

#5 @ayeshrajans
2 years ago

Hi @pcalvo - welcome to the WordPress Trac.
Your note about why this happens is spot-on, but I would argue that the fix should be at the original caller, and now downstream at the esc_textarea function. We have several functions that could trigger this deprecation condition, and we should NOT be going through all of them and cast them to strings.

Can you check backtrace and find out the reason why the $text variable is null in the first place?

<?php
function esc_textarea( $text ) {
  if ($text === null) {
    debug_print_backtrace();
  }
  // ... rest of the function...
  $safe_text = htmlspecialchars( $text, ENT_QUOTES, get_option( 'blog_charset' ) );
        /**

or, you can change the $text parameter to string $text that forces PHP to throw and TypeError if the function is passed a non-string value.

​@fahimmurshed commented on ​PR #7202:


2 years ago
#6

Here's the fix:

function esc_textarea( $text ) {
    // Ensure $text is a string, default to an empty string if null
    $text = is_null($text) ? '' : $text;


    $safe_text = htmlspecialchars( $text, ENT_QUOTES, get_option( 'blog_charset' ) );

    /**
     * Filters a string cleaned and escaped for output in a textarea element.
     *
     * @since 3.1.0
     *
     * @param string $safe_text The text after it has been escaped.
     * @param string $text      The text prior to being escaped.
     */
    return apply_filters( 'esc_textarea', $safe_text, $text );
}
Note: See TracTickets for help on using tickets.