[wp-trac] [WordPress Trac] #45265: REST API: register_rest_route should warn when used improperly.

WordPress Trac noreply at wordpress.org
Fri Jan 11 18:18:46 UTC 2019


#45265: REST API: register_rest_route should warn when used improperly.
--------------------------------------+-----------------------
 Reporter:  kraftbj                   |       Owner:  desrosj
     Type:  defect (bug)              |      Status:  closed
 Priority:  normal                    |   Milestone:  5.1
Component:  REST API                  |     Version:
 Severity:  normal                    |  Resolution:  fixed
 Keywords:  has-patch needs-dev-note  |     Focuses:  rest-api
--------------------------------------+-----------------------
Changes (by desrosj):

 * status:  reviewing => closed
 * resolution:   => fixed


Comment:

 In [changeset:"44568" 44568]:
 {{{
 #!CommitTicketReference repository="" revision="44568"
 REST API: Encourage proper usage of `register_rest_route()`.

 Calling `register_rest_route()` too early in the loading process has the
 potential to cause some unintentional problems and pitfalls. Because
 `register_rest_route()` calls `rest_get_server()` (which creates the
 `WP_REST_Server` instance), calling the function directly and/or before
 `rest_api_init` should be discouraged.

 For example, if `register_rest_route ()` is called on `init`, the REST API
 server instance is set up (and all functions added to `rest_api_init` and
 other related hooks are invoked), even though the current request may not
 be a REST request. Also, if `register_rest_route()` is called even earlier
 (say, in an `mu-plugin` file), required endpoints may be missing since
 normal plugins have not yet been loaded and have not had a chance to
 register their own action hooks.

 This adds a `_doing_it_wrong()` notice the first time
 `register_rest_route()` is called before `rest_api_init` in a request to
 encourage best practices for registering REST API routes.

 Props kraftbj, desrosj, timothyblynjacobs.
 Fixes #45265.
 }}}

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


More information about the wp-trac mailing list