Thanks for the answer.
I already tried the solution from Stackexchange but when I use add_action( 'rest_pre_echo_response'...) my REST api endpoints return NULL
I also tried apply_filters( 'rest_pre_echo_response'...) but that doesn’t do anything.
AFAIK the API ultimately uses the same wp_insert_post() function that the main WP site uses, so you could use the “save_post” action just like you would if saving from the main site. If you need to differentiate between main site saves and API saves, check the request or referrer values in $_SERVER.
Thanks for replying.
I managed to solve the problem. It seems that the action hook was firing when the post was updated via http.post or http.put but the location where I set the JSON file to be saved was wrong so I did not see the file.
I checked in error log and saw that there was an error that the folder wasn’t found. When I set the correct location, everything worked.
Glad you solved the problem. So the right action hook was?
Since I am using Pods plugin for custom posts, this was the correct action hook:
add_action('pods_api_post_save_pod_item_your_pod_name', 'your_function', 10, 3);
-
This reply was modified 7 years, 2 months ago by
marija1206.
Great to hear this @marija1206! So we were still firing like normal, just the issue was in the file location. Would you mind writing that up as a troubleshooting tip? I’d like to add it to our REST API resources on the Pods Docs.
The hook fired normally but the event ended in error log since the desired path where I stored my JSON file was incorrect.
When I updated custom post trough WordPress, the path was correct and my JSON file was saved where I wanted it to be. When the update or new post was created trough the mobile app (http.post or http.put), the function was fired from child theme folder and the path to folder was incorrect.
I set the path $_SERVER['DOCUMENT_ROOT'].'/FOLDER_NAME/JSON_name.json'
So the path to folder was always in the root of WordPress installation and then I could correctly write the path to the folder I wanted the file to be saved.
I don’t know how to write it in technically correct terms but hope this is clear enough.