If you’re a WordPress developer looking to add customizable options to your plugin or theme, learning how to create radio buttons with the WordPress Settings API is essential. The WordPress Settings API provides a standardized way to build settings pages with various input types, including radio buttons. In this article, we’ll explore how to create radio buttons using the Settings API, the types of radio buttons you can implement, and best practices for a user.

Understanding the WordPress Settings API

The WordPress Settings API is a powerful tool designed to simplify the process of adding settings pages, sections, and fields to the WordPress admin dashboard. Instead of manually handling form submissions, validation, and sanitization, the Settings API provides hooks and functions to streamline these tasks, allowing developers to focus on the actual options users will interact with.

Creating radio buttons with the WordPress Settings API involves registering a setting, adding a settings section, and finally adding the radio button fields within that section.

Steps to Create Radio Buttons with WordPress Settings API

1. Register the Setting

Use register_setting() to register your setting group and setting name. This function allows WordPress to handle saving and sanitizing the user input automatically.

function myplugin_register_settings() {
    register_setting(
        'myplugin_options_group',  // Option group
        'myplugin_option_name',    // Option name
        'sanitize_text_field'      // Sanitization callback
    );
}
add_action('admin_init', 'myplugin_register_settings');

2. Add a Settings Section

Settings sections organize your fields logically.

function myplugin_add_settings_section() {
    add_settings_section(
        'myplugin_section_id',         // Section ID
        'My Plugin Settings Section',  // Title
        'myplugin_section_callback',   // Callback function
        'myplugin_settings_page'       // Page slug
    );
}
add_action('admin_init', 'myplugin_add_settings_section');

function myplugin_section_callback() {
    echo '<p>Select your preferred option below:</p>';
}

3. Add Radio Button Fields

Now, you add the radio buttons as settings fields within the section.

function myplugin_add_radio_buttons() {
    add_settings_field(
        'myplugin_radio_field',            // Field ID
        'Choose an option',                // Field Title
        'myplugin_radio_buttons_callback',// Callback to render input
        'myplugin_settings_page',          // Page slug
        'myplugin_section_id'              // Section ID
    );
}
add_action('admin_init', 'myplugin_add_radio_buttons');

function myplugin_radio_buttons_callback() {
    $options = get_option('myplugin_option_name');
    $choices = array(
        'option1' => 'Option 1',
        'option2' => 'Option 2',
        'option3' => 'Option 3',
    );

    foreach ($choices as $value => $label) {
        $checked = checked($options, $value, false);
        echo '<label>';
        echo '<input type="radio" name="myplugin_option_name" value="' . esc_attr($value) . '" ' . $checked . '>';
        echo esc_html($label);
        echo '</label><br>';
    }
}

4. Create the Settings Page

To display these settings, you need an admin menu page.

function myplugin_settings_page() {
    ?>
    <div class="wrap">
        <h1>My Plugin Settings</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('myplugin_options_group');
            do_settings_sections('myplugin_settings_page');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}

function myplugin_add_admin_menu() {
    add_options_page(
        'My Plugin Settings',
        'My Plugin',
        'manage_options',
        'myplugin_settings_page',
        'myplugin_settings_page'
    );
}
add_action('admin_menu', 'myplugin_add_admin_menu');

With these steps, you will have created radio buttons with WordPress Settings API successfully, enabling users to choose among several options easily.

Types of Radio Buttons in WordPress Settings API

Radio buttons can come in several varieties depending on your needs and design preferences:

1. Standard Radio Buttons

These are basic circular radio buttons that allow users to select only one option from multiple choices, as demonstrated in the example above.

2. Styled Radio Buttons

Using CSS, you can customize the appearance of radio buttons to match your plugin or theme’s branding. Custom styling improves user experience but requires additional CSS work.

3. Image Radio Buttons

Instead of text labels, images act as options. This can be achieved by wrapping the radio inputs with images and using CSS and JavaScript for selection feedback.

4. Grouped Radio Buttons

If your settings require categorizing options, you can create grouped radio buttons by adding multiple fields or sections, each containing related radio inputs.

5. Conditional Radio Buttons

Radio buttons can be made conditional, where selecting one reveals or hides additional options. This requires JavaScript to handle the interactivity dynamically.

Best Practices for Creating Radio Buttons with WordPress Settings API

  • Sanitize Input: Always sanitize the selected value to prevent security vulnerabilities.
  • Use checked() Function: Use WordPress’s checked() helper to ensure the correct radio button is selected based on saved options.
  • Clear Labels: Make your option labels clear and descriptive.
  • Accessibility: Use proper labels and consider screen readers for accessibility compliance.
  • Organize Settings: Group radio buttons logically using sections for a clean UI.
  • Documentation: Add instructions or descriptions to guide users on what each option means.

Frequently Asked Questions (FAQs)

What is the WordPress Settings API?

The WordPress Settings API is a set of functions that allow developers to create settings pages, sections, and fields easily. It handles saving, sanitizing, and displaying user options in the WordPress admin.

Why use radio buttons in WordPress settings?

Radio buttons allow users to select a single option from multiple choices, making it perfect for settings that require exclusive selections.

Can I use the Settings API to create other input types besides radio buttons?

Yes, the Settings API supports various input types such as text fields, checkboxes, select dropdowns, and more.

How do I sanitize radio button input?

You can use WordPress’s built-in sanitization functions like sanitize_text_field() or create a custom callback to validate the input.

Can radio buttons be styled or customized?

Yes, with custom CSS and JavaScript, you can style radio buttons or even replace them with image options for better user experience.

Is it necessary to create a settings page to use radio buttons?

While radio buttons can be created anywhere, using the Settings API to create a proper settings page ensures consistent handling and user experience within the WordPress admin.

Conclusion

Creating radio buttons with the WordPress Settings API is a straightforward process that greatly enhances your plugin or theme’s configurability. By following the recommended steps, you can implement various types of radio buttons that fit your design and functionality needs. Leveraging the Settings API not only ensures your options are saved securely and sanitized properly but also provides a user-friendly interface for administrators. Whether you’re creating simple standard radio buttons or advanced conditional ones, the WordPress Settings API offers the flexibility and robustness needed to manage user settings efficiently. Incorporate radio buttons in your settings today to offer clear and easy option selections to your users.

This page was last edited on 29 May 2025, at 9:28 am