Textarea

Description

The textarea field type creates a multi-line text input field in the admin panel, allowing users to enter longer text content. It is rendered as an HTML <textarea> element styled with Bootstrap’s form-control class. This field is ideal for capturing descriptions, notes, or other extended 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 textarea. Yes
label String The display label for the textarea 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 textarea. If set to false, the field will be empty by default. No false
rows Integer The number of visible rows in the textarea, controlling its height. Must be a positive integer. No Browser default
class String Additional CSS classes to apply to the textarea 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 textarea field is stored in the settings array as a sanitized string. The value is sanitized using WordPress’s sanitize_text_field function, which may strip some formatting. For richer text, consider additional sanitization or a different field type.

Example

Below is an example of how to define a textarea field in the $fields array for the Reusable Admin Panel settings class.

$this->fields = array(
    'general' => array(
        array(
            'name'    => __('Description', 'your-plugin-slug'),
            'type'    => 'textarea',
            'label'   => __('Your Description', 'your-plugin-slug'),
            'default' => 'Enter your description here...',
            'rows'    => 6,
            'class'   => 'description-input',
            'help'    => __('Enter a detailed description.', 'your-plugin-slug'),
            'required' => true
        )
    )
);

This configuration will create a textarea field labeled “Your Description” under the “general” section. It will have a default value, be 6 rows tall, include the CSS class description-input, display a help message, and be marked as required.

Usage Notes

  • The textarea field uses WordPress’s sanitize_text_field for sanitization, which removes unsafe characters and line breaks, ensuring the input is safe for storage.
  • The field’s ID is automatically generated as {section}-{name}, where name is sanitized using sanitize_title.
  • The field is styled using Bootstrap’s form-control class. Custom styles can be applied via the class parameter.
  • The rows parameter controls the visible height; if omitted, the browser’s default is used (typically 2-3 rows).
  • The required attribute triggers browser validation, but server-side validation should also be implemented if critical.
  • To retrieve the value of a textarea field, use the get_option method, e.g., $this->get_option('general', 'description').