當(dāng)文章被保存時(shí)觸發(fā)的鉤子。這個(gè)鉤子可以用來保存WordPress后臺(tái)文章編輯內(nèi)的自定義欄目的值到數(shù)據(jù)庫,也就是說當(dāng)你點(diǎn)擊發(fā)布或者更新文章時(shí),你添加的字段會(huì)自動(dòng)更新數(shù)據(jù)庫。
實(shí)例
add_action( 'save_post', 'product_price_save_meta_box' );
function product_price_save_meta_box($post_id){
if ( ! isset( $_POST['product_price_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['product_price_meta_box_nonce'], 'product_price_meta_box' ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['product_price'] ) ) {
return;
}
$product_price = sanitize_text_field( $_POST['product_price'] );
update_post_meta( $post_id, '_product_price', $product_price );
}