WordPress.org

Make WordPress Core

Opened 12 days ago

Last modified 5 days ago

#44564 new defect (bug)

wp_make_link_relative(home_url()) returns empty

Reported by: Fleuv Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.7
Component: Permalinks Keywords: needs-testing
Focuses: Cc:

Description

According to the documentation of the function wp_make_link_relative():

Removes the http or https protocols and the domain. Keeps the path '/' at the beginning, so it isn't a true relative link, but from the web root base.

Regarding, keeping the path '/' at the beginning. This isn't necessarily true when there isn't a path after the web root base (e.g. with the homepage URL this is often the case) resulting in an empty string to be returned.

Consider the test case in the code block below for a WordPress installation what uses the web root base. Thus I can confirm this bug to exists on installation locations such as: http://example.com/ or http://subdomain.example.com/ but not on http://example.com/subdirectory/

The following code will return an empty string.

<?php
echo wp_make_link_relative(home_url());

The expected result is: '/'

Attachments (1)

patch.diff (495 bytes) - added by Fleuv 12 days ago.
Wrap the output of wp_make_link_relative() with trailingslashit() this also does SEO adding a slash to a links without a slash.

Download all attachments as: .zip

Change History (6)

#1 @Fleuv
12 days ago

According to the regex |^(https?:)?//[^/]+(/?.*)|i used in the function, the function should only return empty when the string what passed to it is the web root base and doesn't contain a trailing slash.

@Fleuv
12 days ago

Wrap the output of wp_make_link_relative() with trailingslashit() this also does SEO adding a slash to a links without a slash.

#2 @Fleuv
12 days ago

  • Keywords needs-testing added

#3 in reply to: ↑ description ; follow-up: @SergeyBiryukov
11 days ago

  • Keywords close added; needs-patch removed

Hi @Fleuv, thanks for the ticket!

The following code will return an empty string.

<?php
echo wp_make_link_relative(home_url());

The expected result is: '/'

That's because home_url() generally does not have a trailing slash.

This should work as expected:

echo wp_make_link_relative( home_url( '/' ) );

#4 in reply to: ↑ 3 @Fleuv
5 days ago

Replying to SergeyBiryukov:

That's because home_url() generally does not have a trailing slash.

Same goes for site_url(). Although it is confusing to note "Keeps the path '/' at the beginning" in the documentation if that isn't always true.

Last edited 5 days ago by Fleuv (previous) (diff)

#5 @SergeyBiryukov
5 days ago

  • Keywords close removed

wp_make_link_relative() does keep the slash at the beginning if it's originally there in the passed URL. If not, there is nothing to keep :) See also #26819.

I can see how it's confusing though. Maybe it should add the slash if it's missing after removing the protocol and domain, but be smart enough not to touch the path if it's already relative.

Or, since it's supposed to return a root-relative path rather than a "true" relative path, maybe always adding the missing slash would be expected.

That said, it's not used in core since 2.5, we should search the plugin repo to see how it's used.

Last edited 5 days ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.