这个save_post
钩子是您唯一需要的,而且,您的回调函数缺少一个参数,请检查以下示例:
/**
* Save post metadata when a post is saved.
*
* @param int $post_id The ID of the post.
*/
function save_book_meta( $post_id ) {
/*
* In production code, $slug should be set only once in the plugin,
* preferably as a class property, rather than in each function that needs it.
*/
$slug = \'book\';
// If this isn\'t a \'book\' post, don\'t update it.
if ( $slug != $_POST[\'post_type\'] ) {
return;
}
// - Update the post\'s metadata.
if ( isset( $_REQUEST[\'book_author\'] ) ) {
update_post_meta( $post_id, \'book_author\', sanitize_text_field( $_REQUEST[\'book_author\'] ) );
}
if ( isset( $_REQUEST[\'publisher\'] ) ) {
update_post_meta( $post_id, \'publisher\', sanitize_text_field( $_REQUEST[\'publisher\'] ) );
}
// Checkboxes are present if checked, absent if not.
if ( isset( $_REQUEST[\'inprint\'] ) ) {
update_post_meta( $post_id, \'inprint\', TRUE );
} else {
update_post_meta( $post_id, \'inprint\', FALSE );
}
}
add_action( \'save_post\', \'save_book_meta\' );
示例可以在
Codex page for the save_post
action hook.