0

Can you please look at this demo let me know how I can create and save a checkbox as a custom field in Custom Post Type?

function render_metas( $post )
{
    $we_productMetas = get_post_custom( $post->ID );
    $onSale = isset( $we_productMetas['onSale_box'] ) ? esc_attr( $we_productMetas['onSale_box'][0] ) : '';
?>

<input type="checkbox" class=""  name="onSale_box" id="onSale-box" value="<?php echo $onSale; ?>" />


<?php
}

add_action( 'save_post', 'we_metas_save' );
function we_metas_save( $post_id )
{
if( isset( $_POST['onSale_box'] ) ){
        update_post_meta( $post_id, 'onSale_box', wp_kses( $_POST['onSale_box'], $allowed ) );
    }

}
  • And how do you call render_metas function? Is it bound to some hook? – Krzysiek Dróżdż Sep 25 '18 at 20:10
  • You can use ACF for custom fields. – Pratik Patel Sep 26 '18 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.