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.
When building WordPress plugins or themes, creating user-friendly and flexible settings pages is crucial. One of the common input types you may want to add is the number input, allowing users to enter numeric values conveniently. This article explains how to create number inputs with WordPress Settings API, covering the setup process, the different types of number inputs you can create, and best practices to ensure your inputs are secure.
The WordPress Settings API is a powerful way to create settings pages, sections, and fields in the WordPress admin area. It provides a structured approach for saving, sanitizing, and validating user input in a consistent manner. Using this API ensures your settings integrate seamlessly with the WordPress admin UI and follow WordPress coding standards.
Number inputs are perfect when you want users to enter integer or decimal values, such as quantities, limits, time intervals, or thresholds. The HTML5 <input type="number"> offers built-in validation, supports min/max values, and allows stepping increments, improving user experience and reducing errors.
<input type="number">
To create number inputs, you need to register a setting, add a settings section, and add a settings field with a callback function that outputs the number input field. Here is a step-by-step guide:
Use register_setting() to register your setting. You can also provide a sanitization callback here to validate number input.
register_setting()
function myplugin_register_settings() { register_setting( 'myplugin_settings_group', // Option group 'myplugin_number_option', // Option name 'myplugin_sanitize_number' // Sanitization callback ); } add_action('admin_init', 'myplugin_register_settings'); function myplugin_sanitize_number($input) { return is_numeric($input) ? intval($input) : 0; // sanitize to integer, default 0 }
Create a section on the settings page to organize your fields.
function myplugin_settings_section() { add_settings_section( 'myplugin_main_section', 'Main Settings', 'myplugin_section_callback', 'myplugin_settings_page' ); } add_action('admin_init', 'myplugin_settings_section'); function myplugin_section_callback() { echo '<p>Configure the main settings below.</p>'; }
Add the field using add_settings_field(). The callback outputs the HTML number input.
add_settings_field()
function myplugin_add_number_field() { add_settings_field( 'myplugin_number_option', 'Number Input', 'myplugin_number_field_callback', 'myplugin_settings_page', 'myplugin_main_section' ); } add_action('admin_init', 'myplugin_add_number_field'); function myplugin_number_field_callback() { $value = get_option('myplugin_number_option', 0); echo '<input type="number" id="myplugin_number_option" name="myplugin_number_option" value="' . esc_attr($value) . '" min="0" max="100" step="1" />'; }
Finally, create a settings page where the user can see and save the number input.
function myplugin_settings_page() { ?> <div class="wrap"> <h1>My Plugin Settings</h1> <form method="post" action="options.php"> <?php settings_fields('myplugin_settings_group'); do_settings_sections('myplugin_settings_page'); submit_button(); ?> </form> </div> <?php } function myplugin_add_settings_page() { add_options_page( 'My Plugin Settings', 'My Plugin', 'manage_options', 'myplugin', 'myplugin_settings_page' ); } add_action('admin_menu', 'myplugin_add_settings_page');
With this setup, you will have a number input field on your plugin’s settings page that saves the data securely and validates it on input.
Using the WordPress Settings API and HTML5 number input attributes, you can create various types of number inputs:
step="1"
min
max
step="0.01"
step
echo '<input type="number" id="myplugin_decimal_option" name="myplugin_decimal_option" value="' . esc_attr(get_option('myplugin_decimal_option', '0.00')) . '" min="0" max="10" step="0.01" />';
esc_attr()
Q1: Can I use the WordPress Settings API to create number inputs in custom meta boxes?A: The Settings API is mainly designed for options pages in the admin dashboard. For meta boxes, you typically create number inputs manually using standard HTML inputs and handle saving via post meta functions.
Q2: How do I validate decimal numbers with the WordPress Settings API?A: In your sanitization callback, you can use PHP functions like floatval() or regular expressions to validate and sanitize decimal numbers before saving.
floatval()
Q3: Is it possible to add multiple number inputs in a single settings page?A: Yes, simply register multiple settings fields with unique option names and add them to your settings section. Each field can have its own sanitization callback if needed.
Q4: What if I want to allow negative numbers in my number input?A: Set the min attribute to a negative value (e.g., min="-10") and ensure your sanitization callback allows negative numbers by adjusting validation logic.
min="-10"
Q5: How can I show error messages if users enter invalid numbers?A: The Settings API doesn’t display errors by default, but you can use the add_settings_error() function to show admin notices after validation fails in your sanitization callback.
add_settings_error()
Creating number inputs with WordPress Settings API is a straightforward and effective way to collect numeric data from users in your plugin or theme settings. By leveraging the built-in HTML5 <input type="number"> and the powerful WordPress Settings API, you can ensure your inputs are user-friendly, secure, and properly validated. Remember to use the API’s hooks to register settings, add sections and fields, sanitize inputs, and build your settings page interface. With these tools and best practices, you can create a polished and reliable settings experience tailored to your WordPress project’s needs.
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