[wp-trac] [WordPress Trac] #42807: Add filter to modfiy wp.api versionString

WordPress Trac noreply at wordpress.org
Wed Dec 6 00:51:38 UTC 2017


#42807: Add filter to modfiy wp.api versionString
--------------------------+-----------------------------
 Reporter:  mkaz          |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 We have a need to modify the full REST URL which includes the domain as
 well as appending something after the versionString. The WordPress.com API
 is served at a different URL than the blog is posted, and includes the
 blogId for the request.

 So for self-hosted request to `/wp/v2/post` we would need to be able to
 modify it to `https://public-api.wordpress.com/wp/v2/sites/123456/post`

 There already exists the `rest_url` filter to modify the domain, but the
 versionString `wp/v2/` is hard-coded in most spots and gets tangled up
 with schema and other parts.


 Here is a suggested patch that would add a filter `version_string` which
 would allow modifying the hard-coded part. If no filter defined, it would
 default to the current value.


 {{{
 Index: wp-includes/rest-api.php
 ===================================================================
 +++ wp-includes/rest-api.php    (working copy)
 @@ -367,6 +367,27 @@
  }

  /**
 + * Retrieves the version string for the REST endpoint
 + *
 + * @since 5.0.0
 + * @return string version string, default wp/v2/
 + */
 +function get_rest_version_string() {
 +   $version_string = 'wp/v2/';
 +
 +   /**
 +    * Filters the REST version string.
 +    *
 +    * Use this filter to adjust the url returned by the
 get_rest_version_string() function.
 +    *
 +    * @since 5.0.0
 +    *
 +    * @param string $version_string Version String
 +    */
 +   return apply_filters( 'version_string', $version_string );
 +}
 +
 +/**
   * Retrieves the URL to a REST endpoint.
   *
   * Note: The returned URL is NOT escaped.

 Index: wp-includes/script-loader.php
 ===================================================================
 +++ wp-includes/script-loader.php   (working copy)
 @@ -138,7 +138,7 @@
     did_action( 'init' ) && $scripts->localize( 'wp-api-request',
 'wpApiSettings', array(
         'root'          => esc_url_raw( get_rest_url() ),
         'nonce'         => ( wp_installing() && ! is_multisite() ) ? '' :
 wp_create_nonce( 'wp_rest' ),
 -       'versionString' => 'wp/v2/',
 +       'versionString' => get_rest_version_string(),
     ) );

 }}}

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


More information about the wp-trac mailing list