Make WordPress Core

Opened 4 years ago

Closed 3 years ago

#49146 closed defect (bug) (duplicate)

Corrupted inner REST API requests.

Reported by: vavra7's profile vavra7 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: REST API Keywords: needs-patch
Focuses: rest-api Cc:

Description

1) Make clean installation of WordPress on http://some-wp-domain.com (just example domain - can be random)
2) Go to Settings -> General and change field "Site Address (URL)" to value "http://decoupled-frontend-domain.com". Hit "Save Changes".
3) Go to Pages -> Sample Page make a random change and hit "Update"
4) ERROR

Reason is that WordPress internal request leads to wrong address:

http://decoupled-frontend-domain.com/index.php?rest_route=%2Fwp%2Fv2%2Fpages%2F2&_locale=user

Expected is request pointing to:

http://some-wp-domain.com/index.php?rest_route=%2Fwp%2Fv2%2Fpages%2F2&_locale=user

Wordpress is using "siteurl" domain instead of "home" domain for composing inner REST API request.

Change History (5)

#1 @TimothyBlynJacobs
4 years ago

  • Component changed from General to REST API
  • Focuses accessibility administration removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Severity changed from critical to normal
  • Status changed from new to closed
  • Version changed from 5.3.2 to 4.4

Duplicate of #37020.

Hello @vavra7, welcome to trac!

This is actually intentional behavior. If you need to change this, you can use the rest_url filter in get_rest_url.

See #42869, #46246

#2 @kosso
4 years ago

I have arrived here after having the same issue since we stopped using a 'Classic Editor' plugin and switched to Gutenberg Blocks.

I see the OP mentions "Wordpress is using the site_url host instead of the home host for composing inner REST API requests."

The way I see it is the other way around. Surely?

I thought site_url was the WordPress installation and home was the (or can be a) decoupled site on another domain, being fed by the REST API? Am I wrong?

eg: kosso.mydomain.com is my Wordpress install. local.mydomain.com is a 'decoupled' Angular site.

If I change site_url to local.mydomain.com , then wp-admin 'breaks', with errors caused by styles and scripts being loaded from the wrong domain. (eg: jQuery) ie: the decoupled one.

Also with this example, if I put kosso.mydomain.com in the home option field, my permalinks will also be pointing to the wrong (WP install) host too.

Running Wordpress 5.3.2.

Any info or pointers would be great thanks.

Last edited 4 years ago by kosso (previous) (diff)

#3 @isuke01
4 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

Yes Facing same issue.
I have used WP core files inside wordpress directory so page backend is:
mysite.com/wordpress/wp-admin.

frontend:
mysite.com

I have tried to use

add_filter( 'rest_url', 'fix_rest_url');
 
function fix_rest_url( $url ) {
    return $url;
}

but this actually return correct url mysite.com/wp-json.

Last edited 4 years ago by isuke01 (previous) (diff)

#4 @danielamof
3 years ago

Put this code at the end of functions.php, in your active theme. It works like a charm :) Thanks to Kelderic for the solution: https://github.com/WordPress/gutenberg/issues/1761#issuecomment-549509676

add_filter('rest_url', function($url) {
    $url = str_replace(home_url(), site_url().'/index.php', $url);
    return $url;
});

#5 @TimothyBlynJacobs
3 years ago

  • Resolution set to duplicate
  • Status changed from reopened to closed

Duplicate of #37020.

Reclosing. Using the rest_url filter to adjust this for your particular setup is the recommended way to change this like how @danielamof has done.

Note: See TracTickets for help on using tickets.