The register_setting() WordPress PHP function registers a setting and its data.
Usage
register_setting( $option_group, $option_name, $args );
Example:
register_setting( 'general', 'custom_option_name', array( 'sanitize_callback' => 'sanitize_text_field' ) );
Parameters
- $option_group (string): A settings group name. Default allowed option key names include ‘general’, ‘discussion’, ‘media’, ‘reading’, ‘writing’, and ‘options’.
- $option_name (string): The name of an option to sanitize and save.
- $args (array) – Optional: Data used to describe the setting when registered. Includes:
- type (string): The type of data associated with this setting. Valid values are ‘string’, ‘boolean’, ‘integer’, ‘number’, ‘array’, and ‘object’.
- description (string): A description of the data attached to this setting.
- sanitize_callback (callable): A callback function that sanitizes the option’s value.
- show_in_rest (bool|array): Whether data associated with this setting should be included in the REST API.
- default (mixed): Default value when calling get_option().
More information
See WordPress Developer Resources: register_setting()
Examples
Register a simple text field setting
Registers a new setting called ‘custom_text_field’ in the ‘general’ group.
function myplugin_register_settings() { register_setting( 'general', 'custom_text_field', array( 'sanitize_callback' => 'sanitize_text_field' ) ); } add_action( 'admin_init', 'myplugin_register_settings' );
Register a number field setting
Registers a new setting called ‘custom_number_field’ in the ‘general’ group.
function myplugin_register_settings() { register_setting( 'general', 'custom_number_field', array( 'sanitize_callback' => 'absint' ) ); } add_action( 'admin_init', 'myplugin_register_settings' );
Register a checkbox setting
Registers a new setting called ‘custom_checkbox’ in the ‘discussion’ group.
function myplugin_register_settings() { register_setting( 'discussion', 'custom_checkbox', array( 'sanitize_callback' => 'myplugin_sanitize_checkbox' ) ); } function myplugin_sanitize_checkbox( $input ) { return ( isset( $input ) ? true : false ); } add_action( 'admin_init', 'myplugin_register_settings' );
Register a setting with a custom sanitization function
Registers a new setting called ‘custom_setting’ in the ‘writing’ group with a custom sanitization function.
function myplugin_register_settings() { register_setting( 'writing', 'custom_setting', array( 'sanitize_callback' => 'myplugin_sanitize_custom_setting' ) ); } function myplugin_sanitize_custom_setting( $input ) { // Perform custom sanitization on $input return $sanitized_input; } add_action( 'admin_init', 'myplugin_register_settings' );
Register a setting with REST API support
Registers a new setting called ‘custom_rest_setting’ in the ‘media’ group with REST API support.
function myplugin_register_settings() { register_setting( 'media', 'custom_rest_setting', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', '