WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 6 years ago

#26819 closed defect (bug) (fixed)

wp_make_link_relative() returns domain name instead of zero length string path when no path present

Reported by: adiant Owned by: chriscct7
Milestone: 4.4 Priority: normal
Severity: normal Version: 3.8
Component: Formatting Keywords: has-patch commit
Focuses: Cc:

Description

wp_make_link_relative() simply returns the URL passed to it when the URL has no path, just a domain name. Instead, it should return a zero length string.

A URL with a path of '/' correctly returns '/'.

Incorrect: var_dump( wp_make_link_relative( 'http://www.wordpress.org' ) ) displays "string(24) "http://www.wordpress.org"

Correct: var_dump( wp_make_link_relative( 'http://www.wordpress.org/' ) ) displays string(1) "/"

Workaround: for my purposes, parse_url( $home_url, PHP_URL_PATH ) does all that I need, but wp_make_link_relative() would still be useful when a Query or Fragment is present in the URL.

Attachments (3)

26819.patch (456 bytes) - added by bcworkz 8 years ago.
26819.unittest.diff (564 bytes) - added by MikeHansenMe 7 years ago.
tests wp_make_link_relative
26819.2.patch (464 bytes) - added by chriscct7 6 years ago.

Download all attachments as: .zip

Change History (8)

@bcworkz
8 years ago

#1 @bcworkz
8 years ago

  • Keywords has-patch added

Making the slash in the match expression optional does the trick.

#2 @nacin
8 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

Huh, I never heard of wp_make_link_relative(). Looks like it hasn't been used anywhere in core since WordPress 2.5.

I am fine with fixing this but we will need some unit tests, I think.

@MikeHansenMe
7 years ago

tests wp_make_link_relative

#3 @MikeHansenMe
7 years ago

  • Keywords needs-unit-tests removed

@chriscct7
6 years ago

#4 @chriscct7
6 years ago

  • Keywords commit added
  • Milestone changed from Future Release to 4.4
  • Owner set to chriscct7
  • Severity changed from minor to normal
  • Status changed from new to accepted

#5 @SergeyBiryukov
6 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 35497:

Formatting: wp_make_link_relative() should return an empty string if no path is present in the link.

Props bcworkz, MikeHansenMe, chriscct7, SergeyBiryukov.
Fixes #26819.

Note: See TracTickets for help on using tickets.