register_setting
Register a setting and its data.
Description
Parameters (3)
- 0. $option_group (string)
- A settings group name. Should correspond to a whitelisted option key name. Default whitelisted option key names include general, "discussion," and reading, among others.
- 1. $option_name (string)
- The name of an option to sanitize and save.
- 2. $args — Optional. (array) =>
array()
- Data used to describe the setting when registered.
Options
- type (string) =>
''
The type of data associated with this setting.
- description (string) =>
''
A description of the data attached to this setting.
- sanitize_callback (callable) =>
null
A callback function that sanitizes the option's value.
- show_in_rest (bool) =>
null
Whether data associated with this setting should be included in the REST API.
array( /** * The type of data associated with this setting. * * @type string * @default '' */ 'type' => '', /** * A description of the data attached to this setting. * * @type string * @default '' */ 'description' => '', /** * A callback function that sanitizes the option's value. * * @type callable * @default null */ 'sanitize_callback' => null, /** * Whether data associated with this setting should be included in the REST API. * * @type bool * @default null */ 'show_in_rest' => null );
…
- type (string) =>
Usage
if ( !function_exists( 'register_setting' ) ) { require_once ABSPATH . WPINC . '/option.php'; } // A settings group name. Should correspond to a whitelisted option key name. // Default whitelisted option key names include "general," "discussion," and "reading," among others. $option_group = ''; // The name of an option to sanitize and save. $option_name = ''; // Data used to describe the setting when registered. $args = array( 'type' => '', 'description' => '', 'sanitize_callback' => null, 'show_in_rest' => null ); // NOTICE! Understand what this does before running. $result = register_setting($option_group, $option_name, $args);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/option.php
- function register_setting( $option_group, $option_name, $args = array() ) {
- global $new_whitelist_options, $wp_registered_settings;
- $defaults = array(
- 'type' => 'string',
- 'group' => $option_group,
- 'description' => '',
- 'sanitize_callback' => null,
- 'show_in_rest' => false,
- );
- // Back-compat: old sanitize callback is added.
- if ( is_callable( $args ) ) {
- $args = array(
- 'sanitize_callback' => $args,
- );
- }
- /**
- * Filters the registration arguments when registering a setting.
- *
- * @since 4.7.0
- *
- * @param array $args Array of setting registration arguments.
- * @param array $defaults Array of default arguments.
- * @param string $option_group Setting group.
- * @param string $option_name Setting name.
- */
- $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
- $args = wp_parse_args( $args, $defaults );
- if ( ! is_array( $wp_registered_settings ) ) {
- $wp_registered_settings = array();
- }
- if ( 'misc' == $option_group ) {
- _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
- $option_group = 'general';
- }
- if ( 'privacy' == $option_group ) {
- _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
- $option_group = 'reading';
- }
- $new_whitelist_options[ $option_group ][] = $option_name;
- if ( ! empty( $args['sanitize_callback'] ) ) {
- add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
- }
- if ( array_key_exists( 'default', $args ) ) {
- add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
- }
- $wp_registered_settings[ $option_name ] = $args;
- }