Make WordPress Core

Opened 7 years ago

Closed 4 years ago

#40437 closed enhancement (duplicate)

Better use for home_url filter for external WP-API website

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


Because WordPress encourages the use of WP-API, it's normal that I might have "my external home url" utilising WP-API requests, using permalinks and coded in Angular, React or other JS frameworks.

	add_filter( 'home_url', 'the_real_webapp_home_url', 10, 2);
  1. Not all links pointed to "my external home url" using the home_url filter, eg. navigation links previous_posts_link() and next_posts_link().
  1. The /wp-json rest api endpoint could make some calculations, as only works on the original home_url. Also, most rest api rewrites in parse_request() depends on home_url. As a fix I removed the home_url filter from get_rest_url() and from parse_request().

	 * Fix rest_url after home_url, as rest-api it's still on the main site
	add_filter( 'rest_url', 'kli_cbu_rest_url');
	function kli_cbu_rest_url( $value ){

		remove_filter( 'home_url', 'kli_cbu_home_url', 10, 2);
		remove_filter( 'rest_url', 'kli_cbu_rest_url');

		$url = get_rest_url();

		add_filter( 'home_url', 'kli_cbu_home_url', 10, 2);
		add_filter( 'rest_url', 'kli_cbu_rest_url');
	 	return $url;

	 * Fix parse request
	add_filter( 'do_parse_request', 'kli_cbu_rewrite');
	function kli_cbu_rewrite( $value ){
		remove_filter( 'home_url', 'kli_cbu_home_url', 10, 2);
		add_filter( 'request', function( $value ){ 
			add_filter( 'home_url', 'kli_cbu_home_url', 10, 2);
		 	return $value;

	 	return $value;

Change History (3)

This ticket was mentioned in Slack in #core by noisysocks. View the logs.

4 years ago

#2 @talldanwp
4 years ago

  • Component changed from General to REST API

Hi @klihelp, Thanks for creating this ticket, and apologies it has taken a little while for a response.

So just to be sure, you're talking about using the REST API for a headless site?

How is the situation now, is this still something you're experiencing?

I'm moving the ticket into the 'REST API' component, which will hopefully help get a response.

#3 @TimothyBlynJacobs
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This is a duplicate of #37020. The recommendation if you need this functionality is to use the rest_url filter to adjust the URL used.

Note: See TracTickets for help on using tickets.