元箱复选框值未更新

时间:2015-01-27 14:36:54

标签: wordpress

数据在数据库中正确更新。但是,当我在WP中时,复选框无法正确获取值...它们都显示为未选中状态。关于如何做到这一点的任何想法。 提前致谢

/* 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() {

    add_meta_box(
        '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>Username</th>
<th>    </th>
<th>Visiblity</th>
<th>    </th>
<th>Name</th>
</tr>
<?php
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>";
            unset($ifchecked);

    } ?></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'"));
        }
}

1 个答案:

答案 0 :(得分:0)

而不是:

if(get_post_meta( $object->ID, 'smashing_flautist_access', true ) == $user->user_login) $ifchecked = 'checked="checked" ';

尝试:

$ifchecked = (get_post_meta( $object->ID, 'smashing_flautist_access', true ) == $user->user_login)? 'CHECKED ':'';

使用此功能,您还可以删除unset($ifchecked);,因为您始终在每次迭代中为$ifchecked分配一个新值。 unset不会伤害任何事情,只是没有必要。

HTH,

= C =