[wp-trac] [WordPress Trac] #49749: Bug in WordPress API when I have the prefix wp for the custom endpoint

WordPress Trac noreply at wordpress.org
Wed Apr 1 10:17:53 UTC 2020


#49749: Bug in WordPress API when I have the prefix wp for the custom endpoint
-------------------------------------+------------------------------
 Reporter:  skarabeq                 |       Owner:  (none)
     Type:  defect (bug)             |      Status:  assigned
 Priority:  normal                   |   Milestone:  Awaiting Review
Component:  REST API                 |     Version:  5.4
 Severity:  normal                   |  Resolution:
 Keywords:  needs-patch 2nd-opinion  |     Focuses:
-------------------------------------+------------------------------
Changes (by TimothyBlynJacobs):

 * keywords:  needs-patch => needs-patch 2nd-opinion
 * severity:  major => normal


Comment:

 Thanks @skarabeq,

 The issue here is that you are registering your route with an invalid
 namespace. The namespace must not be prefixed by a forward slash. This
 configuration works.


 {{{#!php
 <?php
 register_rest_route(
         'wp/v2',
         '/nav/',
         array(
                 'methods'  => 'GET',
                 'callback' => static function () {
                         return new WP_REST_Response( null, 204 );
                 },
         )
 );
 }}}

 Now, `register_rest_route` sanitizes this malformed namespace when
 building the final route, but does not pass the sanitized form to the
 server.

 {{{#!php
 <?php
 $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
 rest_get_server()->register_route( $namespace, $full_route, $args,
 $override );
 }}}

 I think this would have resulted in broken behavior previously, but would
 have been much more subtle. I believe your route wouldn't properly appear
 in the index.

 We could fix this in core by doing something like this so the namespace
 parameter is forced to be valid. cc: @kadamwhite

 {{{#!php
 <?php
 $namespace = trim( $namespace, '/' );
 $full_route = '/' . $namespace . '/' . trim( $route, '/' );
 rest_get_server()->register_route( $namespace, $full_route, $args,
 $override );
 }}}

 For now, my recommendation would be to drop the preceding slash.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49749#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list