[wp-trac] [WordPress Trac] #40437: Better use for home_url filter for external WP-API website

WordPress Trac noreply at wordpress.org
Thu Apr 13 14:45:33 UTC 2017


#40437: Better use for home_url filter for external WP-API website
-------------------------+-----------------------------
 Reporter:  klihelp      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 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().


 2. 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;
         }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40437>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list