Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#37177 closed defect (bug) (wontfix)

Incorrect rest_url when home_url contains parameters

Reported by: danielbachhuber's profile danielbachhuber Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: REST API Keywords:
Focuses: Cc:

Description

Problem

When the WordPress home_url contains a parameter, the URL returned by get_rest_url() is wrong.

Example home_url: "http://example.com/?lang=en"
Expected rest url: "http://example.com/wp-json/wp/v2/posts?lang=en"
Actual rest url: "http://example.com/wp-json?lang=en/wp/v2/posts"

Solution

The path has to be passed to get_home_url in its full form, not extended afterwards.

In get_rest_url():

$url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
$url .= '/' . ltrim( $path, '/' );

has to be replaced with:

$path = rtrim( rest_get_url_prefix(), '/' ) . '/' . ltrim( $path, '/' );
$url = get_home_url( $blog_id, $path, $scheme );

Originally https://github.com/WP-API/WP-API/issues/2514

Change History (1)

#1 @danielbachhuber
8 years ago

  • Keywords needs-patch needs-unit-tests removed
  • Milestone 4.6 deleted
  • Resolution set to wontfix
  • Status changed from new to closed
  • Version 4.4 deleted

Actually, this appears to be a problem generally with get_home_url() since r12598:

wp> update_option( 'home', 'http://wordpress-develop.dev/?foo' );
=> bool(true)
wp> get_home_url( null, 'bar' );
=> string(37) "http://wordpress-develop.dev/?foo/bar"

Specifically:

	if ( $path && is_string( $path ) )
		$url .= '/' . ltrim( $path, '/' );

I think supporting query parameters in home opens a huge can of worms, and shouldn't be supported by WordPress. There are other ways of accomplishing the same effect.

Note: See TracTickets for help on using tickets.