}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function artichoke_save_template_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['artichoke_template_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['artichoke_template_meta_box_nonce'], 'artichoke_template_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'artichoke_template' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if (isset( $_POST['artichoke_template_html_field'] ) ) {
$html = sanitize_post_field( 'post_content', $_POST['artichoke_template_html_field'], $post_id, 'raw' );
// unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'artichoke_save_template_meta_box_data' );
// update the post, which calls save_post again
wp_update_post( array( 'ID' => $post_id, 'post_content' => $html ) );
// re-hook this function
add_action( 'save_post', 'artichoke_save_template_meta_box_data' );
}
if (isset( $_POST['artichoke_template_css_field'] ) ) {
$css = sanitize_post_field( 'post_content', $_POST['artichoke_template_css_field'], $post_id, 'raw' );
// Update the meta field in the database.
update_post_meta( $post_id, '_artichoke_template_css_key', $css );
}
}
add_action( 'save_post', 'artichoke_save_template_meta_box_data' );