plugin development – Call API on post save/update and show the result in admin area
After updating/saving a post, I want to call an API and show the response message in the post editor. I tried many things, searched countless websites, and found nothing that works.
This is a simple version of my code, inside a custom plugin (nothing special; just a single PHP file):
function revalidate_post($post_id) {
file_get_contents("https://another-site.com/revalidate/?id=" . $post_id);
// show a message in post editor, and say if operation was successful or not:
// show_message()
}
add_action("save_post", "revalidate_post", 20, 1);
Can somebody please explain how can I do this, step-by-step? The JSON response and things are not my question; I’m stuck in the show_message()
thing.
Some similar questions say that WordPress redirects to another page when the post is published/updated, and thus their solution is to save some value in cookies or URL params, and use that to show a message after the redirection is complete. However, in WordPress 6 and the Block Editor, I don’t see that redirection. Only a simple snackbar (toast notification) appears on the bottom left that says “Post updated/published”.
Leave an answer