Metabox with checkbox is not working true!

admin2025-06-05  2

please help me. i want to show display name of users in a meta box as checkbox. then i can choose many users to display some content in the post by:

if (in_array('special userID', get_post_meta($post->ID, 'U_S_C_users', true)) == true) {
  // Show the content here
  echo "ok";
}

by my code, i see this error:

warning: in_array() expects parameter 2 to be array in string:::: if (in_array($user->ID,$savedusers)){

my codes:

<?php

/* Define the custom box */
add_action('add_meta_boxes', 'User_specific_content_box');

/* Adds a box to the main column on the custom post type edit screens */
function User_specific_content_box() {
    add_meta_box('User_specific_content', __( 'User specific content box'),'User_specific_content_box_inner','class');
}

/* Prints the box content */
function User_specific_content_box_inner() {
    global $post,$wp_roles;
    $savedusers = get_post_meta($post->ID, 'U_S_C_users',true);
    var_dump($savedusers);

    // Use nonce for verification
    wp_nonce_field( plugin_basename(__FILE__), 'User_specific_content_box_inner' );
    echo '<h4>'.__('By User Name:').'</h4>';
    $blogusers = get_users('blog_id=1&orderby=nicename');
    $usercount = 0;
    foreach ($blogusers as $user) {
        echo '<input type="checkbox" name="U_S_C_users" value="'.$user->ID.'"';
        if (in_array($user->ID,$savedusers)){
            echo ' checked';
        }
        echo '>'.$user->display_name.'    ';
        $usercount = $usercount + 1;
        if ($usercount > 5){
            echo '<br/>';
            $usercount = 0;
        }
    }
}


/* Save Meta Box */
add_action('save_post', 'User_specific_content_box_inner_save');

/* When the post is saved, saves our custom data */
function User_specific_content_box_inner_save( $post_id ) {
    global $post;
      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times

      if ( !wp_verify_nonce( $_POST['User_specific_content_box_inner'], plugin_basename(__FILE__) ) )
          return $post_id;

      // verify if this is an auto save routine. 
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
          return $post_id;
      // OK, we're authenticated: we need to find and save the data

    $savedusers = get_post_meta($post_id, 'U_S_C_users',true);

    if (isset($_POST['U_S_C_users']) && !empty($_POST['U_S_C_users'])){
        foreach ($_POST['U_S_C_users'] as $u){
            $new_users[] = $u;
        }
        update_post_meta($post_id, 'U_S_C_users', $new_users);
    }else{
        if (count($savedusers) > 0){
            delete_post_meta($post_id, 'U_S_C_users');
        }
    }
}
?>

my problems:

1- i see that error
2- i want to show just users with author role.

thanks to help me

please help me. i want to show display name of users in a meta box as checkbox. then i can choose many users to display some content in the post by:

if (in_array('special userID', get_post_meta($post->ID, 'U_S_C_users', true)) == true) {
  // Show the content here
  echo "ok";
}

by my code, i see this error:

warning: in_array() expects parameter 2 to be array in string:::: if (in_array($user->ID,$savedusers)){

my codes:

<?php

/* Define the custom box */
add_action('add_meta_boxes', 'User_specific_content_box');

/* Adds a box to the main column on the custom post type edit screens */
function User_specific_content_box() {
    add_meta_box('User_specific_content', __( 'User specific content box'),'User_specific_content_box_inner','class');
}

/* Prints the box content */
function User_specific_content_box_inner() {
    global $post,$wp_roles;
    $savedusers = get_post_meta($post->ID, 'U_S_C_users',true);
    var_dump($savedusers);

    // Use nonce for verification
    wp_nonce_field( plugin_basename(__FILE__), 'User_specific_content_box_inner' );
    echo '<h4>'.__('By User Name:').'</h4>';
    $blogusers = get_users('blog_id=1&orderby=nicename');
    $usercount = 0;
    foreach ($blogusers as $user) {
        echo '<input type="checkbox" name="U_S_C_users" value="'.$user->ID.'"';
        if (in_array($user->ID,$savedusers)){
            echo ' checked';
        }
        echo '>'.$user->display_name.'    ';
        $usercount = $usercount + 1;
        if ($usercount > 5){
            echo '<br/>';
            $usercount = 0;
        }
    }
}


/* Save Meta Box */
add_action('save_post', 'User_specific_content_box_inner_save');

/* When the post is saved, saves our custom data */
function User_specific_content_box_inner_save( $post_id ) {
    global $post;
      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times

      if ( !wp_verify_nonce( $_POST['User_specific_content_box_inner'], plugin_basename(__FILE__) ) )
          return $post_id;

      // verify if this is an auto save routine. 
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
          return $post_id;
      // OK, we're authenticated: we need to find and save the data

    $savedusers = get_post_meta($post_id, 'U_S_C_users',true);

    if (isset($_POST['U_S_C_users']) && !empty($_POST['U_S_C_users'])){
        foreach ($_POST['U_S_C_users'] as $u){
            $new_users[] = $u;
        }
        update_post_meta($post_id, 'U_S_C_users', $new_users);
    }else{
        if (count($savedusers) > 0){
            delete_post_meta($post_id, 'U_S_C_users');
        }
    }
}
?>

my problems:

1- i see that error
2- i want to show just users with author role.

thanks to help me

Share Improve this question asked Dec 7, 2018 at 18:42 sh.dehnavish.dehnavi 411 silver badge12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

That error happened because the $savedusers is not an array — in_array() requires the second parameter be a valid array, and if it's not, that error (or warning) is thrown.

And a simple fix to that is by type-casting the variable to an array like so:

$savedusers = (array) get_post_meta( $post_id, 'U_S_C_users', true );

Or better, use wp_parse_id_list() which makes sure $savedusers is an array of (user) IDs:

$savedusers = wp_parse_id_list( get_post_meta( $post->ID, 'U_S_C_users', true ) );

(You'll make the above change in User_specific_content_box_inner() and User_specific_content_box_inner_save() where get_post_meta() is called.)

Secondly, the checkboxes should be made an array by appending [] to their name like so:

echo '<input type="checkbox" name="U_S_C_users[]" value="'.$user->ID.'"';
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749116869a316505.html

最新回复(0)