[wp-trac] [WordPress Trac] #39345: Request for hook after posts are completely updated via the rest API

WordPress Trac noreply at wordpress.org
Tue Dec 20 17:53:28 UTC 2016


#39345: Request for hook after posts are completely updated via the rest API
-------------------------+------------------------------
 Reporter:  jazbek       |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  REST API     |     Version:  4.7
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:  rest-api
-------------------------+------------------------------

Comment (by jnylen0):

 Hi @jazbek - I wouldn't be opposed to adding a set of actions
 (`rest_processed_*` maybe?) that work as you've described.  However, I
 wonder if either of the following approaches might work for you instead?

 1. If you're using a custom post type, register it with a custom
 `rest_controller_class` that inherits from `WP_REST_Posts_Controller` and
 defines `update_item` and `create_item` methods that call the parent
 method, then perform your custom logic.

 2. Use the [https://github.com/WordPress/WordPress/blob/4.7/wp-includes
 /rest-api/class-wp-rest-server.php#L931 `rest_request_after_callbacks`
 filter] as follows:

 {{{#!php
 <?php
 add_filter( 'rest_request_after_callbacks', function( $response, $handler,
 $request ) {
         if ( is_wp_error( $response ) ) {
                 return $response;
         }

         if (
                 isset( $handler['callback'] ) &&
                 is_callable( $handler['callback'], false, $callable_name )
         ) {
                 switch ( $callable_name ) {
                         case 'WP_REST_Posts_Controller::create_item':
                         case 'WP_REST_Posts_Controller::update_item':
                                 // Get $request['id'] and perform your
 custom logic
                                 break;
                 }
         }

         return $response;
 }, 10, 3 );
 }}}

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


More information about the wp-trac mailing list