pods is an extension of the CPT functionality already built into WordPress, so you’ll just need to hook into the save_post hook, and the write your conditions for what post_type will trigger your desired actions.
Here is the hook reference page: https://codex.ww.wp.xz.cn/Plugin_API/Action_Reference/save_post
Thank you for your reply!
I need to create a webhook which is called when I add a custom post created with Pods. The webhook should trigger a function that creates an endpoint in WP REST API
This is what I tried but it does not work
<?php
function update_all( $data ) {
return rest_ensure_response( 'Hello World, this is the WordPress REST API' );
}
add_action( 'save_post', 'send_update', 10, 3 );
function send_update(){
add_action( 'rest_api_init', 'start_update' );
}
function start_update() {
$datum = date("dmY");
register_rest_route( 'update/', $datum, array(
'methods' => 'GET',
'callback' => 'update_all',
) );
}?>
I want to know if this can be achieved?
Unfortunately, that’s now how the REST API works. If you save a post, it can’t register a REST API route for subsequent use. First, save_post is called after rest_api_init in this regard. Second, register_rest_route only affects the current page load, so it won’t be available when you go to make the separate request.
Thanks for the reply!
Is there a way to update the REST api endpoint with the function send_update called inside the add_action( ‘save_post’, ‘send_update’, 10, 3 );
I know this isn’t a question regarding Pods plugin, but maybe you have an idea on how to achieve this
This isn’t really specific to Pods, but basically you’d need to determine how you want to fetch the data.
When a post is saved, maybe it should save the post ID to a list of IDs as an option in the DB, or flag the post with a meta value. Then on your custom endpoint (registered elsewhere with rest_api_init) you would just respond with the list of IDs that have match the meta flag (or get the IDs from the option). Once the endpoint is called, you can then unset that option / meta. There’s potential for cases where it goes to unset that option/meta but a new post ID was added between when the request started and ended. Those would get lost in that case.
Thanks for the help! I will try to do it that way!
@marija1206 You asked this over in a new forum post, but I think it relates to this discussion:
Is there a way to trigger a webhook when a post is updated via http.put method from the app?
When I update a post within WordPress an action hook add_action(‘pods_api_post_save_pod_item_aktualnost’, ‘send_update’, 10, 3); is triggered but when I update it with http.put method nothing happens.
I tried it in Postman, the update is shown in WordPress custom post but the action hook is not triggered.
Thank you for your reply.
I actually solved the problem I wrote about yesterday and this one is not really related to the that so I opened a new thread.
Yesterday my question was regarding updating post inside WordPress, and I managed to do that but when I update the post via http.put method it doesn’t work.
-
This reply was modified 7 years, 3 months ago by
marija1206.
rest_insert_* is totally separate from the normal saving hooks, it happens after that is done. wp_insert_post() / wp_update_post() still get called and the normal WP hooks still get run.
Is your current problem that the fields you’re trying to set on the Post are not coming through to Pods when saving via REST API? Did you enable the REST API for that Pod and all of it’s fields for read/write access?