Wordpress: executing function when saving or editing post
This is w开发者_Go百科hat I'm trying to accomplish: If I'm adding or editing post I want to run a function that puts post_id, all the custom field values and category ids to some other db table I've created. I just want to know how to execute this function and how to get the values.
Put the following in your functions.php
file of your theme. It will run on both save as well as well update. Since you have the post ID, you can do whatever you want.
function do_my_stuff($post_ID) {
//do my stuff here;
return $post_ID;
}
add_action('save_post', 'do_my_stuff');
This was firing twice.
function do_my_stuff($post_ID) {
//do my stuff here;
return $post_ID;
}
add_action('save_post', 'do_my_stuff');
Adding
if (wp_is_post_revision($post_ID)) return;
to suppress revision solved it for me. Maybe you want to suppress more...
// API request
if (REST_REQUEST) return;
// autosave
if (wp_is_post_autosave($post_ID)) return;
精彩评论