• Hello,

    I am using WordPress to store data which I publish in my mobile app. I am using WP REST API to manage the data displayed and also to post or update to WordPress.

    I am trying to find an action_hook which is fired when someone updates or posts something from the app. I tried multiple add_action functions but none of them was fired when the post was made from the app.

    The app is made with AngularJS and the posts are published with http.post or updated with http.put method.

    Also, I need to mention that the data we are using is made with Pods plugin.

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @marija1206,
    have a look at this Stackexchange issue:
    https://wordpress.stackexchange.com/questions/286307/rest-api-hook-when-post-is-requested

    You may wanto to check the “rest_pre_echo_response” hok as described here:
    https://developer.www.ads-software.com/reference/hooks/rest_pre_echo_response/

    Thread Starter marija1206

    (@marija1206)

    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.

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter marija1206

    (@marija1206)

    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?

    Thread Starter marija1206

    (@marija1206)

    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 5 years, 8 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.

    Thread Starter marija1206

    (@marija1206)

    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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Action hook and REST API’ is closed to new replies.