Skip to content

Replace FILTER_SANITIZE_STRING into better data sanitisation#3185

Merged
dd32 merged 4 commits intoWordPress:trunkfrom
rollybueno:bugfix/depreciated-constant
Mar 19, 2026
Merged

Replace FILTER_SANITIZE_STRING into better data sanitisation#3185
dd32 merged 4 commits intoWordPress:trunkfrom
rollybueno:bugfix/depreciated-constant

Conversation

@rollybueno
Copy link
Copy Markdown
Member

Replace FILTER_SANITIZE_STRING with WordPress Sanitization Functions

Changes Made

  1. Replaced all instances of FILTER_SANITIZE_STRING with WordPress's built-in sanitization functions:

    • sanitize_text_field() for single-line text inputs
    • sanitize_textarea_field() for multi-line text inputs
    • sanitize_email() for email fields
  2. Updated form submission handling in inc/form.php:

    • Replaced filter_input_array() with direct $_POST handling
    • Added proper sanitization callbacks for each field type
    • Maintained array handling for multi-select fields
  3. Updated admin filters in inc/admin.php:

    • Replaced filter_input() calls with sanitize_text_field()
    • Simplified GET parameter handling
    • Maintained default empty string values
  4. Updated post meta handling in inc/post-meta.php:

    • Replaced custom sanitization callback using FILTER_SANITIZE_STRING
    • Now using sanitize_text_field() directly

Why This Change?

  1. FILTER_SANITIZE_STRING is deprecated in PHP 8.1
  2. WordPress's sanitization functions are:
    • Better maintained
    • More secure
    • Consistent with WordPress coding standards
    • Not affected by PHP version changes
    • Specifically designed for WordPress data

Testing

The changes maintain the same functionality while improving security and future compatibility. All form submissions, admin filters, and post meta handling should work as before but with better sanitization.

!! Since we don't have testunit, this still needs a manual testing. Make sure that it has been tested thoroughly before merging!

Related Issues

  • Addresses PHP 8.1 deprecation warnings
  • Improves security by using WordPress's recommended sanitization methods
  • Makes the code more maintainable and consistent with WordPress standards

Issue reported: #2525

dd32 and others added 3 commits March 19, 2026 16:22
WordPress adds slashes to superglobals - wp_unslash() must be called
before sanitize_text_field/sanitize_textarea_field/sanitize_email to
avoid leaving stray backslashes in user input.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…onstant

# Conflicts:
#	wp-content/plugins/wporg-learn/inc/form.php
#	wp-content/plugins/wporg-learn/inc/post-meta.php
@dd32
Copy link
Copy Markdown
Member

dd32 commented Mar 19, 2026

I commited some changed in #3394, I'm going to merge your PR over the top of it :)

@dd32 dd32 merged commit 9cdeabd into WordPress:trunk Mar 19, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants