Experience the powerful AI writing right inside WordPress
Show stunning before-and-after transformations with image sliders.
Improve user engagement by showing estimated reading time.
Written by saedul
Showcase Designs Using Before After Slider.
If you’re a WordPress developer looking to create user-friendly settings pages, the WordPress Settings API is your best friend. In this article, we will explore how to create checkboxes with WordPress Settings API, covering the different types and how to implement them correctly. This approach ensures your settings pages are secure, scalable, and easy to maintain.
The WordPress Settings API provides a standardized way to create settings pages, sections, and fields within the WordPress admin dashboard. It handles form rendering, validation, and data saving, which saves you from writing repetitive code. When you create checkboxes using the Settings API, you get a clean, consistent UI and seamless data management.
Checkboxes are an intuitive way for users to toggle features or options on and off. For example, enabling/disabling comments, showing/hiding certain UI elements, or activating custom functionality. Using checkboxes with WordPress Settings API lets you:
Let’s break down the steps to create checkboxes with WordPress Settings API.
You first need to add a settings page where the checkbox will appear.
add_action('admin_menu', 'myplugin_add_settings_page'); function myplugin_add_settings_page() { add_options_page( 'My Plugin Settings', 'My Plugin', 'manage_options', 'myplugin-settings', 'myplugin_render_settings_page' ); }
Use register_setting(), add_settings_section(), and add_settings_field() functions to define your settings.
register_setting()
add_settings_section()
add_settings_field()
add_action('admin_init', 'myplugin_settings_init'); function myplugin_settings_init() { register_setting('myplugin_options_group', 'myplugin_options', 'myplugin_options_validate'); add_settings_section( 'myplugin_main_section', 'Main Settings', 'myplugin_section_text', 'myplugin-settings' ); add_settings_field( 'myplugin_checkbox', 'Enable Feature', 'myplugin_checkbox_render', 'myplugin-settings', 'myplugin_main_section' ); } function myplugin_section_text() { echo '<p>Configure the main settings below:</p>'; }
Now, create a callback function to output the checkbox HTML. This function retrieves the saved option and sets the checkbox as checked or unchecked.
function myplugin_checkbox_render() { $options = get_option('myplugin_options'); ?> <input type="checkbox" name="myplugin_options[checkbox]" value="1" <?php checked(1, isset($options['checkbox']) ? $options['checkbox'] : 0); ?> /> <label for="myplugin_options[checkbox]">Check to enable the feature</label> <?php }
Always sanitize input to maintain security.
function myplugin_options_validate($input) { $new_input = array(); if (isset($input['checkbox'])) { $new_input['checkbox'] = absint($input['checkbox']); } else { $new_input['checkbox'] = 0; // unchecked } return $new_input; }
Finally, output the form for settings on your page.
function myplugin_render_settings_page() { ?> <div class="wrap"> <h1>My Plugin Settings</h1> <form action="options.php" method="post"> <?php settings_fields('myplugin_options_group'); do_settings_sections('myplugin-settings'); submit_button(); ?> </form> </div> <?php }
With the WordPress Settings API, you can create various checkbox types depending on your needs:
A simple checkbox toggling one option on or off. This is the example shown above.
When you want users to select multiple options from a list.
function myplugin_checkbox_group_render() { $options = get_option('myplugin_options'); $choices = array( 'option_1' => 'Option 1', 'option_2' => 'Option 2', 'option_3' => 'Option 3' ); foreach ($choices as $key => $label) { ?> <input type="checkbox" name="myplugin_options[checkbox_group][]" value="<?php echo esc_attr($key); ?>" <?php if (isset($options['checkbox_group']) && in_array($key, $options['checkbox_group'])) { echo 'checked="checked"'; } ?> /> <label><?php echo esc_html($label); ?></label><br> <?php } }
In this case, validation should handle an array of inputs.
You can use CSS or JavaScript to transform the checkbox into a toggle switch but the underlying code remains the same. This enhances UX but requires frontend styling.
absint()
sanitize_text_field()
checked()
You can save multiple checkbox values by using an array input. Each checkbox should have the same name attribute with square brackets (e.g., name="myplugin_options[checkbox_group][]"). In validation, ensure you process the array and sanitize each value.
name="myplugin_options[checkbox_group][]"
Yes. The Settings API supports different input types including text fields, checkboxes, radio buttons, and dropdowns. You just need to write appropriate rendering and validation functions for each type.
The Settings API handles nonce verification, option sanitization, form fields generation, and database storage securely and consistently. It prevents common mistakes and improves compatibility with other plugins and WordPress itself.
While the Settings API handles the backend part, the frontend appearance of checkboxes can be customized using CSS or JavaScript. For example, you can create toggle switches or styled checkboxes for better UX.
Use the checked() function when rendering the checkbox to reflect saved values. Also, sanitize and validate the input properly before saving it to the database.
Creating checkboxes with WordPress Settings API is a reliable and efficient way to add toggles and options to your plugin or theme settings page. By following the proper steps registering settings, rendering fields, and sanitizing inputs you ensure your settings are user-friendly and secure. Whether you need single checkboxes, checkbox groups, or custom-styled toggles, the WordPress Settings API provides a solid foundation. With this knowledge, you can enhance your WordPress projects and provide better control to your users.
This page was last edited on 29 May 2025, at 9:28 am
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
How many people work in your company?Less than 1010-5050-250250+
By proceeding, you agree to our Privacy Policy