[wp-trac] [WordPress Trac] #53656: Not possible to get a post type's REST API namespace if modified

WordPress Trac noreply at wordpress.org
Mon Oct 11 12:29:55 UTC 2021


#53656: Not possible to get a post type's REST API namespace if modified
--------------------------+-----------------------------
 Reporter:  swissspidy    |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Future Release
Component:  REST API      |     Version:  4.7
 Severity:  normal        |  Resolution:
 Keywords:  needs-patch   |     Focuses:
--------------------------+-----------------------------
Description changed by swissspidy:

Old description:

> It's possible to supply a custom `rest_controller_class` to
> `register_post_type()`. A post type's controller class can be retrieved
> using `WP_Post_Type::get_rest_controller()`. Same goes for taxonomies &
> `WP_Taxonomy`.
>
> Now, such a custom controller can potentially change the `namespace` and
> `rest_base` for the post type, like so:
>
> {{{#!php
> <?php
> class My_Controller extends WP_REST_Posts_Controller {
>         public function __construct() {
>                 parent::__construct();
>                 $this->namespace = 'foo/v1';
>         }
> }
>
> register_post_type('bar', array( 'rest_controller_class' =>
> My_Controller::class ) );
> }}}
>
> However, there's now no way to get the post type's REST API base URL
> (`wp-json/foo/v1/bar`.
>
> For example, `/wp-json/wp/v2/types/bar will link to `/wp-json/wp/v2/bar`
> because the `wp/v2` namespace is hardcoded in
> `WP_REST_Post_Types_Controller`.
>
> Is there a way to get a post type's updated namespace and use that in
> `WP_REST_Post_Types_Controller` and other places?
>
> Hardcoding everything to `wp/v2` seems a bit limiting.
>
> Or is the namespace perhaps not intended to be modified like this?

New description:

 It's possible to supply a custom `rest_controller_class` to
 `register_post_type()`. A post type's controller class can be retrieved
 using `WP_Post_Type::get_rest_controller()`. Same goes for taxonomies &
 `WP_Taxonomy`.

 Now, such a custom controller can potentially change the `namespace` and
 `rest_base` for the post type, like so:

 {{{#!php
 <?php
 class My_Controller extends WP_REST_Posts_Controller {
         public function __construct() {
                 parent::__construct();
                 $this->namespace = 'foo/v1';
         }
 }

 register_post_type('bar', array( 'rest_controller_class' =>
 My_Controller::class ) );
 }}}

 However, there's now no way to get the post type's REST API base URL (`wp-
 json/foo/v1/bar`.

 For example, `/wp-json/wp/v2/types/bar` will link to `/wp-json/wp/v2/bar`
 because the `wp/v2` namespace is hardcoded in
 `WP_REST_Post_Types_Controller`.

 Is there a way to get a post type's updated namespace and use that in
 `WP_REST_Post_Types_Controller` and other places?

 Hardcoding everything to `wp/v2` seems a bit limiting.

 Or is the namespace perhaps not intended to be modified like this?

--

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


More information about the wp-trac mailing list