Make WordPress Core

Opened 13 years ago

Closed 11 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
Priority: normal Milestone: 4.4
Component: Formatting Version: 3.8
Severity: normal Keywords: has-patch commit
Cc: Focuses:

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 13 years ago.
26819.unittest.diff​ (564 bytes ) - added by MikeHansenMe 12 years ago.
tests wp_make_link_relative
26819.2.patch​ (464 bytes ) - added by chriscct7 11 years ago.

Download all attachments as: .zip

Change History (8)

@bcworkz
13 years ago

#1 @bcworkz
13 years ago

  • Keywords has-patch added

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

#2 @nacin
13 years ago

  • Keywords needs-unit-tests added
  • Milestone Awaiting Review → 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
12 years ago

tests wp_make_link_relative

#3 @MikeHansenMe
12 years ago

  • Keywords needs-unit-tests removed

@chriscct7
11 years ago

#4 @chriscct7
11 years ago

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

#5 @SergeyBiryukov
11 years ago

  • Resolution → fixed
  • Status accepted → 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.