WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My question is in regard to this article:


This article:

  1. creates a custom metabox
  2. the custom metabox queries 'subscriber' users
  3. saves the metabox value as a radio button

What I would like to know is if anyone knows how:

  1. change the metabox to multiple checkboxes (pretty straightforward)
  2. save the multiple checkbox values in one field, separated by commas (i.e. an array).

The code: (in functions.php)

/* Fire our meta box setup function on the post editor screen. */
add_action( 'load-post.php', 'smashing_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'smashing_post_meta_boxes_setup' );

/* Meta box setup function. */
function smashing_post_meta_boxes_setup() {

    /* Add meta boxes on the 'add_meta_boxes' hook. */
    add_action( 'add_meta_boxes', 'smashing_add_post_meta_boxes' );

    /* Save post meta on the 'save_post' hook. */
    add_action( 'save_post', 'smashing_flautist_access_save_meta', 10, 2 );

/* Create one or more meta boxes to be displayed on the post editor screen. */
function smashing_add_post_meta_boxes() {

        'smashing-flautist-access',         // Unique ID
        esc_html__( 'Post Viewing Permission', 'smashing_flautist' ),       // Title
        'smashing_flautist_access_meta_box',        // Callback function
        'destinations',                 // Admin page (or post type)
        'normal',                   // Context
        'default'                   // Priority

/* Display the post meta box. */
function smashing_flautist_access_meta_box( $object, $box ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'smashing_flautist_access_nonce' ); ?>

<table class="smashing-flautist-access">
<tr align="left">
<th>    </th>
<th>    </th>
global $post;
    $users = get_users('role=subscriber');
    foreach ($users as $user) {
            $user_info = get_userdata( $user->ID );
            if(get_post_meta( $object->ID, 'smashing_flautist_access', true ) == $user->user_login) $ifchecked = 'checked="checked" ';
            echo "<tr>";
            echo "<td>$user->user_login</td><td>    </td>";
            echo "<td align=\"center\"><input type=\"checkbox\" name=\"smashing-flautist-access\" id=\"smashing-flautist-access\" value=\"$user->user_login\" " . $ifchecked ."/></td><td>  </td>";
            echo "<td>$user_info->last_name, $user_info->first_name</td><td>    </td>";
            echo "</tr>";

    } ?></table>

<?php }

/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'smashing_flautist_access_save_meta', 10, 2 );

/* Save the meta box's post metadata. */
function smashing_flautist_access_save_meta( $post_id, $post ) {

    /* Make all $wpdb references within this function refer to this variable */
    global $wpdb;

    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST['smashing_flautist_access_nonce'] ) || !wp_verify_nonce( $_POST['smashing_flautist_access_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST['smashing-flautist-access'] ) ? sanitize_html_class( $_POST['smashing-flautist-access'] ) : '' );

    /* Get the meta key. */
    $meta_key = 'smashing_flautist_access';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );
        $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'private' WHERE ID = ".$post_id." AND post_type ='post'"));
    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );
        $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'private' WHERE ID = ".$post_id." AND post_type ='post'"));
    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );
        $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'public' WHERE ID = ".$post_id." AND post_type ='post'"));
share|improve this question

closed as off-topic by s_ha_dum Feb 20 '14 at 15:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

This is a pure HTML problem. All of your checkboxes have the same name. Use smashing-flautist-access[] instead-- notice the []. You will get an array for that value that you can then process and save as you wish. – s_ha_dum Feb 20 '14 at 15:02
makes sense, to which lines would you be referring? – Ryan Dorn Feb 20 '14 at 15:20
Where you are generating the markup-- inside the smashing_flautist_access_meta_box function. – s_ha_dum Feb 20 '14 at 15:22
ok, that's what i thought you meant. just to be specific and confirm, the name=\"smashing-flautist-access\" would become name=\"smashing-flautist-access[]\" i've changed this and see that the data gets updated correctly in the the database. however, when i'm in WP, the checkboxes don't get the values correctly...they all show up as unchecked. any thoughts on how to do this? – Ryan Dorn Feb 20 '14 at 15:43
Now you have a WordPress-ish problem. See: wordpress.stackexchange.com/a/126556/21376 – s_ha_dum Feb 20 '14 at 15:50

Browse other questions tagged or ask your own question.