$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Save metabox with multiple checkbox array|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Save metabox with multiple checkbox array

matteradmin7PV0评论

I have a requirement where the number of checkboxes outputted in the metabox is not fixed. Had it been fixed, I could've followed any one of the tutorials on the internet which suggests I name each checkbox and save it using update_post_meta()

In my case, I have named the checkbox in array notation as name = 'multval[]' since the number of checkboxes is unknown.

I'm failing to understand how to use:

  1. get_post_meta()
  2. checked()
  3. update_post_meta()

when it comes to accessing checkboxes when it is named as an array.

The Code:

<?php  
add_action( 'add_meta_boxes', function() {
    add_meta_box( 'custom-metabox', 'Select values', 'fill_metabox', 'post', 'normal' );
});

function fill_metabox( $post ) {
    wp_nonce_field( basename(__FILE__), 'mam_nonce' );

    // How to use 'get_post_meta()' for multiple checkboxes as array?

    $elements = get_elements(); //returns an associative array
    foreach ( $elements as $element) {
        ?>

        <p>
            <input 
                type  = "checkbox"
                name  = "multval[]"
                value = <?php echo $element;?>
                //How to use checked() function in case of 
                //multiple checkbox as arrays
            >
            <?php echo $element;?>
        </p>

        <?php
    }
}


add_action( 'save_post', function( $post_id ) {
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'mam_nonce' ] ) && wp_verify_nonce( $_POST[ 'mam_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    /*How to run a loop to save values for each checkbox?*/
});

I have a requirement where the number of checkboxes outputted in the metabox is not fixed. Had it been fixed, I could've followed any one of the tutorials on the internet which suggests I name each checkbox and save it using update_post_meta()

In my case, I have named the checkbox in array notation as name = 'multval[]' since the number of checkboxes is unknown.

I'm failing to understand how to use:

  1. get_post_meta()
  2. checked()
  3. update_post_meta()

when it comes to accessing checkboxes when it is named as an array.

The Code:

<?php  
add_action( 'add_meta_boxes', function() {
    add_meta_box( 'custom-metabox', 'Select values', 'fill_metabox', 'post', 'normal' );
});

function fill_metabox( $post ) {
    wp_nonce_field( basename(__FILE__), 'mam_nonce' );

    // How to use 'get_post_meta()' for multiple checkboxes as array?

    $elements = get_elements(); //returns an associative array
    foreach ( $elements as $element) {
        ?>

        <p>
            <input 
                type  = "checkbox"
                name  = "multval[]"
                value = <?php echo $element;?>
                //How to use checked() function in case of 
                //multiple checkbox as arrays
            >
            <?php echo $element;?>
        </p>

        <?php
    }
}


add_action( 'save_post', function( $post_id ) {
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'mam_nonce' ] ) && wp_verify_nonce( $_POST[ 'mam_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    /*How to run a loop to save values for each checkbox?*/
});
Share Improve this question asked Sep 24, 2016 at 8:53 Siddharth ThevarilSiddharth Thevaril 6178 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 7

Try the following code.

add_action( 'add_meta_boxes', function() {
    add_meta_box( 'custom-metabox', 'Select values', 'fill_metabox', 'post', 'normal' );
});

function fill_metabox( $post ) {
    wp_nonce_field( basename(__FILE__), 'mam_nonce' );

    // How to use 'get_post_meta()' for multiple checkboxes as array?
    $postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) );

    // Our associative array here. id = value
    $elements = array(
        'apple'  => 'Apple',
        'orange' => 'Orange',
        'banana' => 'Banana'
    );

    // Loop through array and make a checkbox for each element
    foreach ( $elements as $id => $element) {

        // If the postmeta for checkboxes exist and 
        // this element is part of saved meta check it.
        if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) {
            $checked = 'checked="checked"';
        } else {
            $checked = null;
        }
        ?>

        <p>
            <input  type="checkbox" name="multval[]" value="<?php echo $id;?>" <?php echo $checked; ?> />
            <?php echo $element;?>
        </p>

        <?php
    }
}


add_action( 'save_post', function( $post_id ) {
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'mam_nonce' ] ) && wp_verify_nonce( $_POST[ 'mam_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    // If the checkbox was not empty, save it as array in post meta
    if ( ! empty( $_POST['multval'] ) ) {
        update_post_meta( $post_id, 'elements', $_POST['multval'] );

    // Otherwise just delete it if its blank value.
    } else {
        delete_post_meta( $post_id, 'elements' );
    }

});
Post a comment

comment list (0)

  1. No comments so far