Skip to content

Formatting: Prevent PHP 8.1 deprecation when passing null to esc_url() - #12132

Open
Dervish12 wants to merge 2 commits into
WordPress:trunkfrom
Dervish12:trac/esc-url-null-deprecation
Open

Formatting: Prevent PHP 8.1 deprecation when passing null to esc_url()#12132
Dervish12 wants to merge 2 commits into
WordPress:trunkfrom
Dervish12:trac/esc-url-null-deprecation

Conversation

@Dervish12

@Dervish12 Dervish12 commented Jun 9, 2026

Copy link
Copy Markdown

Summary

Fix a PHP 8.1+ deprecation notice in esc_url() when null is passed as the $url parameter.

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

On PHP 8.1+, passing null to esc_url() triggers:

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated

This occurs because esc_url() calls ltrim( $url ) on line 4545 without first checking for null. Multiple core functions that return string|null (such as get_edit_post_link(), get_edit_comment_link(), get_edit_bookmark_link()) have their return values passed directly to esc_url() throughout the codebase.

Fix

Add null === $url to the existing empty check, returning an empty string. This matches the pre-PHP 8.1 behavior where ltrim(null) silently returned ''.

Follow-up to [r62034] which fixed the same pattern in previous_posts().

Tests

  • test_null_input_returns_empty_string — verifies esc_url( null ) returns ''
  • test_sanitize_url_null_input_returns_empty_string — verifies sanitize_url( null ) returns ''

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Add a null check to `esc_url()` to prevent the "Passing null to
parameter WordPress#1 ($string) of type string is deprecated" notice that
occurs on PHP 8.1+ when null is passed to `ltrim()` on line 4545.

Multiple WordPress core functions such as `get_edit_post_link()`,
`get_edit_comment_link()`, and `get_previous_posts_page_link()` can
return null, and their return values are frequently passed directly
to `esc_url()` without intermediate null checks.

While the individual callers can be guarded (as done in [r62034] for
`previous_posts()`), hardening `esc_url()` itself provides defense in
depth for all current and future callers, including third-party code.

When null is passed, `esc_url()` now returns an empty string, which
matches the existing behavior on PHP < 8.1 (where `ltrim(null)` silently
returned an empty string).

Adds unit tests to validate the behavior for both `esc_url()` and
`sanitize_url()`.

Follow-up to [r62034].

Props Dervish12.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props dervishov.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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.

1 participant