admin管理员组

文章数量:1287776

I created a simple plugin. There is an options page and on it, all existing user roles are displayed. Here is the code:

 <h2>Select user roles to send notifications:</h2>
 <?php
   global $wp_roles;
   $roles = $wp_roles->get_names();
   foreach($roles as $role) { $i++; ?>

    <div class="role-wrapper">
    <input name="role[]" type="checkbox" id="user_role-<?php echo $i; ?>" value="<?php echo $role;?>">
    <label for="user_role-<?php echo $i; ?>"><?php echo $role;?></label>
    </div>

 <?php } ?>

After the administrator marks the necessary roles in the checkboxes and clicks the submit button, the selected roles are saved to the database.

  if(!empty($_POST['role'])) {
     $new_user_roles = array();
     foreach($_POST['role'] as $key=>$value){
         $new_user_roles[$key] = $value;
     }
     update_option('choosed_users_role', $new_user_roles);
   }

Then everything works as it should. But, I don't know how to display the selected checkboxes. After refreshing the page, you cannot see which checkboxes were selected.

I created a simple plugin. There is an options page and on it, all existing user roles are displayed. Here is the code:

 <h2>Select user roles to send notifications:</h2>
 <?php
   global $wp_roles;
   $roles = $wp_roles->get_names();
   foreach($roles as $role) { $i++; ?>

    <div class="role-wrapper">
    <input name="role[]" type="checkbox" id="user_role-<?php echo $i; ?>" value="<?php echo $role;?>">
    <label for="user_role-<?php echo $i; ?>"><?php echo $role;?></label>
    </div>

 <?php } ?>

After the administrator marks the necessary roles in the checkboxes and clicks the submit button, the selected roles are saved to the database.

  if(!empty($_POST['role'])) {
     $new_user_roles = array();
     foreach($_POST['role'] as $key=>$value){
         $new_user_roles[$key] = $value;
     }
     update_option('choosed_users_role', $new_user_roles);
   }

Then everything works as it should. But, I don't know how to display the selected checkboxes. After refreshing the page, you cannot see which checkboxes were selected.

Share Improve this question asked Sep 14, 2021 at 4:37 Victor SokoliukVictor Sokoliuk 1397 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can use get_option() to retrieve the selected role(s) that were saved in the database, then use checked() in your checkboxes. E.g.

// In your code, after this line:
$roles = $wp_roles->get_names();

// Add this which retrieves and stores the selected role(s):
$selected_roles = (array) get_option( 'choosed_users_role' );

/* And then in your <input>, use checked() with in_array():
<input name="role[]" type="checkbox" id="user_role-<?php echo $i; ?>" value="<?php echo $role;?>"
  <?php checked( in_array( $role, $selected_roles ) ); ?>>
*/

Additional Notes

  • If you used the settings API to create the options page, you wouldn't need to manually call update_option() to save the selected role(s).

  • $i is never declared in your code, so you should declare it before the foreach begins.

  • WP_Roles::get_names() returns an associative array with role slug and name pairs, e.g. 'administrator' => 'Administrator' or 'foo_role' => 'Foo Role', so I suggest you to save the role slug and not the name.

  • I would use wp_roles() like so: $roles = wp_roles()->get_names(), which means you don't need to access the global $wp_roles variable.

本文标签: plugin developmentHow leave selected checkboxes marked after they are selected and saved in the database