Adam Projo Adam Projo - 1 month ago 5
Javascript Question

Wordpress Custom field Radio Button cant checked

i ve been try to make radio button in my field, like this :



function orderMB_meta_box_output( $post ) {
// create a nonce field
wp_nonce_field( 'my_orderMB_meta_box_nonce', 'orderMB_meta_box_nonce' ); ?>
<p>
<label><b>Status Order :</b></label>
<br />
<input type="radio" name="status_order" value="Process Packing" <?php echo ($value[0] == 'Process Packing')? 'checked="checked"':''; ?> >Process Packing<br>
<input type="radio" name="status_order" value="Shipping" <?php echo ($value[0] == 'Shipping')? 'checked="checked"':''; ?> >Shipping<br>
<input type="radio" name="status_order" value="Arrive" <?php echo ($value[0] == 'Arrive')? 'checked="checked"':''; ?> >Arrive<br>
<input type="radio" name="status_order" value="Success" <?php echo ($value[0] == 'Success')? 'checked="checked"':''; ?> >Success<br>
</p>
<?php
}

function orderMB_meta_box_save( $post_id ) {
// Stop the script when doing autosave
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

// Verify the nonce. If insn't there, stop the script
if( !isset( $_POST['orderMB_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['orderMB_meta_box_nonce'], 'my_orderMB_meta_box_nonce' ) ) return;

// Stop the script if the user does not have edit permissions
if( !current_user_can( 'edit_post' ) ) return;
update_post_meta( $post_id, 'orderno', esc_attr( $_POST['orderno'] ) );
$allowed = array('Process Packing','Shipping','Arrive','Success');

if( isset( $_POST['status_order'] ) && in_array($_POST['status_order'], $allowed))
update_post_meta( $post_id, 'status_order', esc_attr( $_POST['status_order'] ) );
}
add_action( 'save_post', 'orderMB_meta_box_save' );




I think my code is correct and its work, but in why my radio button wont to checked.

have someone help me ?

Answer

You've missed the line to get values from database which stored against current post id..

$status_order = get_post_meta( $post->ID, 'status_order', true );

Your function should have above line to get value & check it by var_dump(). remove var_dump if value is perfectly showing / checked your radio buttons.

function orderMB_meta_box_output( $post ) {
  // create a nonce field
  wp_nonce_field( 'my_orderMB_meta_box_nonce', 'orderMB_meta_box_nonce' ); 
    $status_order = get_post_meta( $post->ID, 'status_order', true );
    var_dump($status_order); // dump to check database value
    ?>
 <p>
        <label><b>Status Order :</b></label>
        <br />
        <input type="radio" name="status_order" value="Process Packing" <?php echo ($status_order == 'Process Packing')? 'checked="checked"':''; ?> >Process Packing<br>
        <input type="radio" name="status_order" value="Shipping" <?php echo ($status_order == 'Shipping')? 'checked="checked"':''; ?> >Shipping<br>
        <input type="radio" name="status_order" value="Arrive" <?php echo ($status_order == 'Arrive')? 'checked="checked"':''; ?> >Arrive<br>
        <input type="radio" name="status_order" value="Success" <?php echo ($status_order == 'Success')? 'checked="checked"':''; ?> >Success<br>
    </p>
  <?php
}