wordpress-fieldmanager
wordpress-fieldmanager copied to clipboard
TextArea and RichTextArea fields cause a warning in PHP 8.1
Description of the bug
Empty FieldManager_TextArea
fields (e.g., ones on new post pages, or ones that have never been given a value) show a warning in PHP 8.1+.
This appears to be happening because esc_textarea()
is run on the value when rendering the textarea, and the esc_textarea()
function uses htmlspecialchars()
, and htmlspecialchars()
no longer accepts null
without a warning as of PHP 8.1.
A workaround: if the FieldManager_TextArea
is initiated with a default_value
set to ''
(empty string), this warning does not happen, presumably because then the htmlspecialchars()
function runs on an empty string instead of null
.
Steps To Reproduce
-
Configure a field manager with a textarea field, like this example adapted from the FieldManager homepage:
add_action( 'fm_post_post', function() { $fm = new Fieldmanager_Group( array( 'name' => 'contact_information', 'children' => array( 'name' => new Fieldmanager_TextField( 'Name' ), 'description' => new Fieldmanager_TextArea( 'Description' ), ), ) ); $fm->add_meta_box( 'Contact Information', 'post' ); } );
-
Load the page in an environment running PHP 8.1.
-
Notice the error in the PHP error log:
PHP Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /wp/wp-includes/formatting.php on line 4732
Additional Information
When I created this ticket, I thought this warning was only happening for textareas in repeatable groups, but it actually applies to all textareas.
It appears this issue was fixed via https://github.com/alleyinteractive/wordpress-fieldmanager/pull/866 (see file diff), and so I'm going to close it.