Make WordPress Core

Opened 3 weeks ago

Last modified 3 weeks ago

#63252 new defect (bug)

$tax_url with no path not accounted for in /wp-includes/canonical.php

Reported by: domainsupport's profile domainsupport Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.7.2
Component: Canonical Keywords: has-patch
Focuses: php-compatibility Cc:

Description

In the redirect_canonical() function in /wp-includes/canonical.php, on line 346 the $tax_url is obtained by ...

<?php
$tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy );

... which can then be filtered with the term_link hook.

The $tax_url is then parsed on line 375 with ...

<?php
$tax_url = parse_url( $tax_url );

So, if the $tax_url is filtered with the term_link hook such as the new URL doesn't have a path (such as "https://example.com") then the following errors are observed ...

[07-Apr-2025 19:45:25 UTC] E_WARNING: Undefined array key "path" in /wp-includes/canonical.php on line 383
[07-Apr-2025 19:45:25 UTC] E_DEPRECATED: basename(): Passing null to parameter #1 ($path) of type string is deprecated in /wp-includes/canonical.php on line 537
[07-Apr-2025 19:45:25 UTC] E_DEPRECATED: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /wp-includes/canonical.php on line 619

To fix this, line 377 needs to be changed from ...

<?php
                                                        if ( ! empty( $tax_url['query'] ) ) {
                                                                // Taxonomy accessible via ?taxonomy=...&term=... or any custom query var.
                                                                parse_str( $tax_url['query'], $query_vars );
                                                                $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
                                                        } else {
                                                                // Taxonomy is accessible via a "pretty URL".
                                                                $redirect['path'] = $tax_url['path'];
                                                        }

... to ...

<?php
                                                        if ( ! empty( $tax_url['query'] ) ) {
                                                                // Taxonomy accessible via ?taxonomy=...&term=... or any custom query var.
                                                                parse_str( $tax_url['query'], $query_vars );
                                                                $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
                                                        } elseif (isset($tax_url['path'])) {
                                                                // Taxonomy is accessible via a "pretty URL".
                                                                $redirect['path'] = $tax_url['path'];
                                                        } else {
                                                                $redirect['path'] = '';
                                                        }

This is observed with PHP v8.1 onwards.

Change History (1)

This ticket was mentioned in PR #8664 on WordPress/wordpress-develop by @snehapatil02.


3 weeks ago
#1

  • Keywords has-patch added; needs-patch removed

This PR fixes an issue in the redirect_canonical() function in wp-includes/canonical.php where URLs without a path component would cause PHP warnings.

The fix adds an explicit check for the existence of the 'path' key before trying to access it and provides a fallback empty path when it's missing.

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

Note: See TracTickets for help on using tickets.