skip to Main Content

I’m making multiple checkboxes in post admin panel. And when user create post and check necessary checkbox it stored it to output in post page.
My code for creating and saving checkboxes.

function expertise_checkbox_meta_box_callback( $post, $meta ){
    $screens = $meta['args']; 
    wp_nonce_field( plugin_basename(__FILE__), 'expertise_checkbox_field_noncename' );
    $value = maybe_unserialize(get_post_meta( $post->ID, 'expertise_checkbox_elements', true ));
    //"id" => "value"
    $expertise_checkbox_elements = array(
        'design_research'  => pll__('Design research'),
        'product_design' => pll__('Product design'),
        '3d_modelling' => pll__('3D modelling'),
        'engineering' => pll__('Engineering'),
        'dfm' => pll__('Design for manufacturing'),
        'prototyping' => pll__('Prototyping'),
        'production' => pll__('Production')
    );
    foreach ( $expertise_checkbox_elements as $id => $element ) {
        // If the value for checkboxes exist and 
        // this element is part of saved meta check it.
        if ( is_array( $value ) && in_array( $id, $value ) ) {
            $checked = 'checked="checked"';
        } else {
            $checked = null;
        }
        ?>
        <p>
            <input type="checkbox" name="multval[]" value="<?php echo $id;?>" <?php echo $checked; ?> />
            <?php echo $element;?>
            <br>
        </p>
        <?php
    }
}
##Save
add_action( 'save_post', 'client_company_save_postdata' );
function client_company_save_postdata( $post_id ) {
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'expertise_checkbox_field_noncename' ] ) && wp_verify_nonce( $_POST[ 'expertise_checkbox_field_noncename' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    if ( $is_revision || !$is_valid_nonce )
        return; 
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;
    if( ! current_user_can( 'edit_post', $post_id ) )
        return;
    if ( ! empty( $_POST['multval'] ) ) {
        update_post_meta( $post_id, 'expertise_checkbox_elements', $_POST['multval'] );
    // Otherwise just delete it if its blank value.
    } else {
        delete_post_meta( $post_id, 'expertise_checkbox_elements' );
    }
}

And this code worked (it’s save checkbox status and update it). But when I output array of the checkbox values at post page I see design_research instead of "Design research", dfm instead of "Design for manufacturing" and etc.
Code for output values.

<?php 
$expertise_elements = (get_post_meta( $post->ID, 'expertise_checkbox_elements', true )); 
foreach ($expertise_elements as $element) {
   echo ('<li>'.$element.'</li>');
}
?>

I see that I store "keys" from array, not values. But I can’t understand how to make this code output "values" from array.

2

Answers


  1. See the answer I provided here:
    https://wordpress.stackexchange.com/questions/416533/get-custom-field-label-select-dropdown-on-front-end/416534#416534

    You can set the keys of the array to be user-friendly, but more appropriate would be to define the options array in a function, and then use that function in both the backend and frontend.

    Login or Signup to reply.
  2. Just use the same array you had initially, as a map to names.
    Something like this:

    $names = array(
      'design_research'  => __('Design research'),
      'product_design' => __('Product design'),
      '3d_modelling' => __('3D modelling'),
      'engineering' => __('Engineering'),
      'dfm' => __('Design for manufacturing'),
      'prototyping' => __('Prototyping'),
      'production' => __('Production')
    );
    $expertise_elements = (get_post_meta($post->ID, 'expertise_checkbox_elements', true));
    foreach ($expertise_elements as $el) {
      echo ('<li>' . $names[$el] . '</li>');
    }
    

    Depending on the rest of your code, and to not repeat yourself, you can have that array as class variable, a function or a user-provided list of values/names.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search