WordPress.org

Make WordPress Core

Opened 2 years 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: General 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 (0)

Note: See TracTickets for help on using tickets.