在我的自定义插件中,我有一个自定义的帖子类型和一个自定义的元框。我有几个字段,其中一个是复选框。我希望在选择时默认选中此复选框add new post
然后继续选择用户ie已选中/未选中。
我正在添加相关代码,即:用于metabox以及如何保存它。
function coupon_add_metabox() {
add_meta_box( \'coupon_details\', __( \'Coupon Details\', \'domain\' ), \'wpse_coupon_metabox\', \'coupons\', \'normal\', \'high\' );
}
function wpse_coupon_metabox( $post ) {
// Retrieve the value from the post_meta table
$coupon_status = get_post_meta( $post->ID, \'coupon_status\', true );
// Checking for coupon active option.
if ( $coupon_status ) {
if ( checked( \'1\', $coupon_status, false ) )
$active = checked( \'1\', $coupon_status, false );
}
// Html for the coupon details
$html = \'\';
$html .= \'<div class="coupon-status"><label for="coupon-status">\';
$html .= __( \'Active\', \'domain\' );
$html .= \'</label>\';
$html .= \'<input type="checkbox" id="coupon-status-field" name="coupon-status-field" value="1"\' . $active . \' /></div>\';
echo $html;
}
function wpse_coupons_save_details( $post_id ) {
// 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 $post_id;
// Check the user\'s permissions.
if ( ! current_user_can( \'activate_plugins\' ) )
return $post_id;
$coupon_status = sanitize_text_field( $_POST[\'coupon-status-field\'] );
// Update the meta field in the database.
update_post_meta( $post_id, \'coupon_status\', $coupon_status );
}
add_action( \'save_post\', \'wpse_coupons_save_details\' );