How to save Metabox checkbox with multiple values

Asked

Viewed 583 times

2

I have a loop where it shows the taxonomies of the groups in the format of checkbox, but when he saves, he saves only the last checkbox marked.

I would like to know how to save all fields checked?

<?php
function noticias_dados_meta_box($post){
    $values = get_post_custom( $post->ID );
    $grupos = isset($values['grupos']) ? esc_attr($values['grupos'][0]) : '';
    wp_nonce_field( 'novos_dados_cliente', 'dados_cliente_nonce' );
?> 

    <!-- checkbox GRUPOS -->
    <label for="grupos">Grupos</label> <br> <br>
<?php 
        $categories=get_categories('title_li=&taxonomy=grupo');  
        foreach($categories as $category) { 
            if ($category->term_id == $grupos) {
                echo "<input type='checkbox' name=‘grupos[]' value='$grupos' checked='true' />";
                echo $category->cat_name;
                echo '<br>';   
            } 
            else{ 
                echo "<input type='checkbox' name=‘grupos[]' value='$category->term_id' />"; 
                echo $category->cat_name;
                echo '<br>';    
            }
        }
}// fecha a função noticias_dados_meta_box

add_action( 'save_post', 'noticias_dados_meta_box_save' );
function noticias_dados_meta_box_save( $post_id ){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;        
    if( !isset( $_POST['dados_cliente_nonce'] ) || !wp_verify_nonce( $_POST['dados_cliente_nonce'], 'novos_dados_cliente' ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;

    if( isset( $_POST['grupos']))
        update_post_meta( $post_id, 'grupos', $_POST['grupos']);
}
?>

2 answers

3

To send fields with multiple values it is necessary to add [] at the name. Thus grupos will be an array and then just a foreach to access all the values.

echo "<input type='checkbox' name='grupos[]' value='$grupos' checked='true' />";

The foreach will update each checkbox:

if( isset( $_POST['grupos'])){
   foreach($_POST['grupos'] as $valor_grupo){
      update_post_meta( $post_id, 'grupos', $valor_grupo);
   }
}
  • Hello 'lost'! The foreach you are talking about is this: foreach($Categories as $Category)? Because when I add [ ] no value is saved.

  • No, the foreach is to retrieve checkboxes marked.

  • I still could not do. I am not good in php, I am starting now. I do not know where to insert this other foreach to recover the data.

  • This foreach you put in the file that in the tag action <form>.

  • I don’t have the <form> tag because I’m using Metabox to show the checkbox.

2

Change your foreach:

if ($category->term_id == $grupos) {
    echo "<input type='checkbox' name='grupos[]' value='$grupos' checked='true' />";
    echo $category->cat_name;
    echo '<br>';   
} 
else{ 
    echo "<input type='checkbox' name='grupos[]' value='$category->term_id' />"; 
    echo $category->cat_name;
    echo '<br>';    
}

The change made was to add [] the front of the name grupos saying with that to that same name (groups) there may be more than one value.

So your answer (for this request / POST for example), will be something like (if you run a var_dump($_POST['grupos']) or print_r($_POST['grupos']).

Array
(
    [0] => 0
    [1] => 5
    [2] => 13
)

Being 0, 5, 13 the values selected.

From there just treat this return and save in the desired way.

Note: of course this everything I presented above is being done in the most raw/manual way possible, not using any WP helper/Function

EDITED

What you can do on the page where you save is the following (or something like):

$grupos = $_POST['grupos'];

foreach ($grupos as $value) {
  // aqui você mantém o código que executava o save anteriormente
  // PORÉM troca o valor/variavel antigamente adicionada por $value
}

If possible, add your code here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.