Make WordPress Core

Opened 23 months ago

Last modified 15 months ago

#44152 new defect (bug)

Comments "up" link does not cover custom post types and/or REST controllers

Reported by: dsifford Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: REST API Keywords:
Focuses: rest-api Cc:


Currently, WP_REST_Comments_Controller only covers built-in post types under the /wp/v2/ namespace when making "up" links.


Note that wp/v2 is hard-coded on the line above.

This causes issues if a comment falls under a custom post type with it's own REST controller falling under a different namespace.

Change History (3)

#1 @danielbachhuber
23 months ago

@dsifford How would you suggest solving this issue?

#2 @dsifford
23 months ago

@danielbachhuber Not totally sure. I'm only very loosely familiar with the internals of the API.

I went in and fixed my singular issue by hooking into rest_prepare_comment and adjusting for the specific post type by doing a similar hard-coding of the endpoint.

One specific thing that tripped me up in the process of doing this is that it doesn't look like it's currently possible to obtain the namespace of a given object type easily (if at all).

The only thing that it appears you have access to is the rest_base and the rest_controller_class which, although still very useful and helpful, only allows someone to build the REST url after the namespace itself.

Digging into WP_REST_Server, I saw that there is a get_namespaces method and an associated get_namespace_index method, but it ostensibly only allows you to obtain an endpoint's namespace if you have a WP_REST_Request instance that is actively hitting that endpoint available. We don't have that in this case.

So yeah, I'm a bit stumped here. Happy to work with you and others on this though if needed.

Best case scenario I'd say would be to add a rest_namespace field to the custom post type. That way, a full REST url could be built from the object itself, rather than just the part after the namespace.

That, or implement a public getter method for the namespace of WP_REST_Controller.

Last edited 23 months ago by dsifford (previous) (diff)

#3 @pento
15 months ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.