Wordpress action hook on rest api update

4.4k Views Asked by At

I made an app which uses Wordpress REST API to fetch data and users can manipulate the data and add their own posts from the app.

The app is written in AngularJS and uses http.post method to add a post and http.put to update the existing post. We are using custom posts which are made with Pods plugin.

I am trying to find an action hook which would fire whenever a user makes a http.post or http.put request to the REST API.

So far I tried:

function on_all_status_transitions( $new_status, $old_status, $post ) {
  if ( $new_status != $old_status ) {
    // A function to perform actions any time any post changes status.
  }
}
add_action(  'transition_post_status',  'on_all_status_transitions', 10, 3 );

And also: add_action(save_post)

Thanks in advance!

2

There are 2 best solutions below

3
On

Have you tried rest_post_dispatch Hook?

Allows modification of the response before returning:

https://developer.wordpress.org/reference/hooks/rest_post_dispatch/

0
On

I solved the problem

Turns out the folder where I should save JSON file was not set correctly which caused an error. I checked the error log and that is where I saw what the problem is.

After setting the path to folder correctly the function triggered with action hook worked.

This is the hook I use to trigger the function when the post is updated or a new post is added from the app using http.post or http.put methods:

add_action('pods_api_post_save_pod_item_your_pod_name', 'your_function', 10, 3);

I am using Pods plugin for custom post and this is the action hook from their documentation.