我在WordPress上创建了一个角色复选框。我成功地生成了它们,但checked函数似乎不起作用。
此外,它还发出了这一警告。
Warning: Illegal string offset \'ue_roles_confirm_Administrator\'
这是我正在使用的代码。
function username_editor_roles_callback() {
global $wp_roles;
$option = get_option( \'username_editor_settings\' );
$roles = $wp_roles->roles;
foreach ($roles as $role) {
$roleName = $role[\'name\'];
$output = sprintf(\'<input type="checkbox" name="username_editor_settings[ue_roles_confirm_%1$s]" value="%1$s" %2$s><label>%1$s</label><br>\',
$roleName,
checked(1, $option["ue_roles_confirm_{$roleName}"])
);
echo $output;
}
}
我找不出它为什么不起作用。我阅读了有关使用带有设置API的数组的所有问题,但遗憾的是,我没有了解他们在做什么。
我的主要想法是检查是否选中了哪个字段。例如,选中administrator和editor。
提前感谢
NOTE: The values are successfully saved in the database inside the wp_options table.Update: I solved the checkbox part by using this method
由于这个问题:Saving multiple checkboxes with WordPress settings api
function username_editor_roles_callback() {
global $wp_roles;
$roles = $wp_roles->roles;
foreach ($roles as $role) {
$roleName = $role[\'name\'];
$output = sprintf(\'<input type="checkbox" name="username_editor_settings[ue_roles_confirm][]" value="%1$s" %2$s><label>%1$s</label><br>\',
$roleName,
checked( in_array($roleName, ue_settings_option()["ue_roles_confirm"]), 1, false )
);
echo $output;
}
}
But the warning issue still appears if I uncheck all the boxes.
Solved: I solved all the problems. Adding the working function in answer. If there is a better solution I will still accept the answer even the issue is fixed