Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#44564 closed defect (bug) (wontfix)

wp_make_link_relative(home_url()) returns empty

Reported by: fleuv's profile Fleuv Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.7
Component: Permalinks Keywords: close
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 6 years 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 (8)

#1 @Fleuv
6 years 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
6 years 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
6 years ago

  • Keywords needs-testing added

#3 in reply to: ↑ description ; follow-up: @SergeyBiryukov
6 years 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
6 years 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 6 years ago by Fleuv (previous) (diff)

#5 @SergeyBiryukov
6 years 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 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.

Version 0, edited 6 years ago by SergeyBiryukov (next)

#6 @Fleuv
6 years ago

  • Keywords close added; needs-testing removed
  • Resolution set to wontfix
  • Status changed from new to closed

Oke thank you for the reply. I will check for an empty string and add the slash manually.

#7 @netweb
6 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.