#44564 closed defect (bug) (wontfix)
wp_make_link_relative(home_url()) returns empty
Reported by: | 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)
Change History (8)
@
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.
#3
in reply to:
↑ description
;
follow-up:
↓ 4
@
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
@
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.
#5
@
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 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.
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.