'Call Web API to Update Custom Field on Post Save in Wordpress
I need to grab some data from a web API and stick it in a custom field when the user hits update in Wordpress. I have a js file that correctly gets the API data and am enqueueing the script with the code below. The problem is timing. The fetch call obviously comes in too late and the post is saved before I can add it to the custom field. Not sure how to set this up?
if ( $hook == 'post-new.php' || $hook == 'post.php' ) { if ( $post->post_type == 'speakers' ) { wp_enqueue_script( 'test', get_stylesheet_directory_uri() . '/js/test.js?v=111', 'jquery', '', true ); } }
Plus - I have to make the API call in the browser, not server side.
Any help would be amazing! Thanks!
Solution 1:[1]
You can try the pre_post_update action to process your API data before the save, this will work only for updating posts, not creating.
function do_something_before_updating_post($post_id, $post_data) {
// Run your API
}
add_action('pre_post_update', 'do_something_before_updating_post', 10, 2);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
