WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 2 weeks ago

#40437 new enhancement

Better use for home_url filter for external WP-API website

Reported by: klihelp Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8
Component: REST API Keywords:
Focuses: Cc:

Description

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 (2)

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


2 weeks ago

#2 @talldanwp
2 weeks 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.

Note: See TracTickets for help on using tickets.