Make WordPress Core

Opened 7 years ago

Closed 4 years ago

#41775 closed enhancement (invalid)

REST API: Add filter to allow creation of custom comment type

Reported by: langan's profile langan Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: REST API Keywords:
Focuses: Cc:


When trying to create a comment using the REST API that has a different comment type I get the following error.

Cannot create a comment with that type.

Looks like it is coming from this code.

// Do not allow comments to be created with a non-default type.
if ( ! empty( $request['type'] ) && 'comment' !== $request['type'] ) {
        return new WP_Error( 'rest_invalid_comment_type', __( 'Cannot create a comment with that type.' ), array( 'status' => 400 ) );

Looks like this was added to fix defect #38820

Would it be possible to change this check to use a filter that will allow some comment types? Something like this

if ( ! empty( $request['type'] ) && false === in_array($request['type'], apply_filters( 'rest_allow_comment_types', array('comment'), $request ) ) {
        return new WP_Error( 'rest_invalid_comment_type', __( 'Cannot create a comment with that type.' ), array( 'status' => 400 ) );

We could then use the same filter in the update_item call as well I've attached a diff with a first draft idea

Attachments (1)

allow-comment-types.diff (3.1 KB) - added by langan 7 years ago.
Diff of changes to class-wp-rest-comments-controller.php

Download all attachments as: .zip

Change History (4)

7 years ago

Diff of changes to class-wp-rest-comments-controller.php

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.

5 years ago

#2 @TimothyBlynJacobs
5 years ago

  • Keywords close added
  • Milestone Awaiting Review deleted

Hi @langan,

I'm sorry for the delay in your ticket receiving a response.

At the moment, the recommended path forward from #38820 is to use a custom endpoint for custom comment types until #35214 lands and we can support custom comment types natively.

Adding the filter here may make it difficult to support custom comment types in the future properly, since custom comment types will likely have different schema and permission requirements.

Unfortunately, the REST API controllers are currently a bit difficult to extend/reuse which is something I'm hoping to look at soon. The way to keep track of that would be in the REST API meetings in Make Slack.

#3 @desrosj
4 years ago

  • Keywords close removed
  • Resolution set to invalid
  • Status changed from new to closed

Closing this one out per @TimothyBlynJacobs's recommendation. invalid is just Trac's unfriendly way of saying there were no actions that could be taken here.

Note: See TracTickets for help on using tickets.