最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

options - Multiple checkbox doesn't work in wordpress settings api

programmeradmin2浏览0评论

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. Update: I solved the checkbox part by using this method

Thanks to this question: 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

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. Update: I solved the checkbox part by using this method

Thanks to this question: 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

Share Improve this question edited Nov 14, 2020 at 13:35 Raashid Din asked Nov 14, 2020 at 7:25 Raashid DinRaashid Din 2182 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Here is the function I used to solve the problem

function username_editor_roles_callback() {
    global $wp_roles;
    $roles = $wp_roles->roles;

    foreach ($roles as $role) {
        $roleName = $role['name'];
        $output = sprintf('<input type="checkbox" id="ue_roles_checkbox" name="username_editor_settings[ue_roles_confirm][]" value="%1$s" %2$s><label for="ue_roles_checkbox">%1$s</label><br>', 
            $roleName,
            checked( in_array($roleName, (array) ue_settings_option()["ue_roles_confirm"]), 1, false )
        );
        echo $output;
    }
}
发布评论

评论列表(0)

  1. 暂无评论