Multiple checkbox doesn’t work in wordpress settings api

Question

I was creating a checkbox of roles n WordPress. I successfully generated them but the checked function doesn’t seem to work.

Also, it is throwing this warning.

Warning: Illegal string offset 'ue_roles_confirm_Administrator' 

Here is the code I am using.

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;
    }
}

I am unable to find why it doesn’t work. I read all the questions regarding using an array with settings API but unfortunately, I didn’t get what they are doing.

My main idea is to check which field is checked or not. For example, administrator and editor are checked.

Thanks in advance

NOTE: The values are successfully saved in the database inside the wp_options table.

0
Raashid Din 2 months 0 Answers 13 views 0

Leave an answer