Description
The text
field type creates a single-line text input field in the admin panel, allowing users to enter free-form text. It is rendered as an HTML <input type="text">
element styled with Bootstrap’s form-control class. This field is ideal for capturing strings such as usernames, titles, or other short text data.
Available Options
Parameter | Type | Description | Required | Default |
---|---|---|---|---|
name |
String | The name of the field, used as the identifier and default label if label is not provided. |
Yes | – |
type |
String | The field type. Must be set to text . |
Yes | – |
label |
String | The display label for the text field. If not provided, the name is used. |
No | name |
placeholder |
String | A hint displayed in the field when it is empty, guiding the user on expected input. | No | name |
default |
String/Boolean | The default value of the text field. If set to false , the field will be empty by default. |
No | false |
class |
String | Additional CSS classes to apply to the text input for custom styling or JavaScript targeting. | No | – |
help |
String | A help message displayed in a sidebar or tooltip when the user clicks the info icon next to the field. | No | – |
required |
Boolean | If true , the field is marked as required, adding the HTML required attribute. |
No | false |
Note: The text
field is stored in the settings array as a sanitized string. The value is sanitized using WordPress’s sanitize_text_field
function to ensure security.
Example
Below is an example of how to define a text
field in the $fields
array for the Reusable Admin Panel settings class.
$this->fields = array(
'general' => array(
array(
'name' => __('Username', 'your-plugin-slug'),
'type' => 'text',
'label' => __('Your Username', 'your-plugin-slug'),
'default' => '',
'class' => 'username-input',
'help' => __('Enter a username.', 'your-plugin-slug'),
'required' => true
)
)
);
This configuration will create a text field labeled “Your Username” under the “general” section. It will be empty by default, have the CSS class username-input
, include a help message, and be marked as required.
Usage Notes
- The
text
field uses WordPress’ssanitize_text_field
for sanitization, removing unsafe characters and ensuring the input is safe for storage. - The field’s ID is automatically generated as
{section}-{name}
, wherename
is sanitized usingsanitize_title
. - The field is styled using Bootstrap’s
form-control
class. Custom styles can be applied via theclass
parameter. - The
required
attribute triggers browser validation, but server-side validation should also be implemented if critical. - To retrieve the value of a text field, use the
get_option
method, e.g.,$this->get_option('general', 'username')
.