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 badges1 Answer
Reset to default 2You 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 theforeach
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 development - How leave selected checkboxes marked after they are selected and saved in the database 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741321986a2372256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论