These are the foundational fields for collecting simple text, numeric, and sensitive data. They cover the most common use cases for any settings page.
All fields are added to a section using the add_field()
method. The common parameters are:
add_field( string $id, string $section_id, string $type, string $title, array $extras = [] )
This guide focuses on the $type
and the available $extras
for each basic input field.
extras
The following keys can be used in the $extras
array for most basic input fields:
'description'
(string): Text displayed below the field to provide context or instructions.'default'
(mixed): The default value for the field if no value has been saved yet.'html_attributes'
(array): An associative array of HTML attributes to add to the input element (e.g., ['placeholder' => 'Enter text...']
).'conditions'
(array): An array of rules to conditionally show or hide this field. (See the Conditional Logic guide for details).text
sanitize_text_field()
$page->add_field(
'company_name',
'general_section',
'text',
'Company Name',
[
'description' => 'Enter the name of your business.',
'default' => 'My Awesome Company',
'html_attributes' => [
'placeholder' => 'e.g., Stark Industries',
],
]
);
url
url
input type, which may provide special validation or keyboard layouts on mobile devices.esc_url_raw()
$page->add_field(
'privacy_policy_url',
'general_section',
'url',
'Privacy Policy URL',
[
'description' => 'Link to your website\'s privacy policy.',
'html_attributes' => [
'placeholder' => 'https://example.com/privacy',
],
]
);
email
email
input type for browser-level validation.sanitize_email()
$page->add_field(
'support_email',
'general_section',
'email',
'Support Email',
[
'description' => 'The email address where customers can reach you for support.',
'default' => get_option('admin_email'),
]
);
number
$page->add_field(
'items_per_page',
'display_section',
'number',
'Items Per Page',
[
'description' => 'How many items to show on a single page.',
'default' => 10,
'html_attributes' => [
'min' => 1,
'max' => 50,
'step' => 1,
],
]
);
password
********
) with the same length as the saved value. This gives the user a visual confirmation that a value is set without revealing it.$page->add_field(
'api_secret_key',
'api_section',
'password',
'API Secret Key',
[
'description' => 'Your secret key is stored securely and is never shown here.',
'html_attributes' => [
'autocomplete' => 'new-password', // Helps prevent browser auto-fill
],
]
);
textarea
sanitize_textarea_field()
$page->add_field(
'custom_css',
'advanced_section',
'textarea',
'Custom CSS',
[
'description' => 'Add custom CSS rules here. They will be loaded on the front-end.',
'html_attributes' => [
'rows' => 8,
'cols' => 50,
'placeholder' => '.my-class { color: red; }',
],
]
);
color
wp-color-picker
scripts and styles.#f00
or #ff0000
).$page->add_field(
'primary_brand_color',
'display_section',
'color',
'Primary Brand Color',
[
'description' => 'Select the primary color for your theme.',
'default' => '#3858e9',
]
);
With these basic fields, you can build the foundation of most settings pages. The next guide will explore fields designed for making choices, like toggles, radio buttons, and dropdowns.