register_setting( string $option_group, string $option_name, array $args = array() )
- Since
- 2.7.0, 3.0.0, 3.5.0, 4.7.0, 5.5.0, 6.6.0
- Source
wp-includes/option.php:2996
Compatibility
- WordPress
- since 6.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$option_groupstring- A settings group name. Should correspond to an allowed option key name.
Default allowed option key names include 'general', 'discussion', 'media', 'reading', 'writing', and 'options'. $option_namestring- The name of an option to sanitize and save.
$argsarrayoptional- Data used to describe the setting when registered.Default:
array()$typestringThe type of data associated with this setting. Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.$labelstringA label of the data attached to this setting.$descriptionstringA description of the data attached to this setting.$sanitize_callbackcallableA callback function that sanitizes the option's value.$show_in_restbool|arrayWhether data associated with this setting should be included in the REST API. When registering complex settings, this argument may optionally be an array with a 'schema' key.$defaultmixeddefault: value when calling get_option()
Performance profile
How much work a call to register_setting() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 56–117
- Plugin surface
- 2 hooks
- Called by
- 7
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 117.
Third-party callbacks on 'register_setting_args', 'register_setting' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost register_setting() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$option_group !== "misc" && $option_group !== "privacy" && empty($args) && !$args | 56–69 | is_callable(), apply_filters(), wp_parse_args(), do_action() |
$option_group !== "misc" && $option_group !== "privacy" | 63–76 | is_callable(), apply_filters(), wp_parse_args(), add_filter(), do_action() |
empty($args) && !$args | 69–82 | is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), do_action() |
$option_group !== "misc" && $option_group !== "privacy" && !empty($args) && $args | 70–83 | is_callable(), apply_filters(), wp_parse_args(), add_filter(), add_filter(), do_action() |
$option_group !== "misc" && $option_group !== "privacy" && empty($args) && !$args | 70–77 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), do_action() |
| always | 76–89 | is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), add_filter(), do_action() |
$option_group !== "misc" && $option_group !== "privacy" | 77–84 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), add_filter(), do_action() |
$option_group === "misc" && $option_group === "privacy" && empty($args) && !$args | 82–95 | is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), do_action() |
!empty($args) && $args | 83–96 | is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), add_filter(), add_filter(), do_action() |
empty($args) && !$args | 83–90 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), do_action() |
$option_group !== "misc" && $option_group !== "privacy" && !empty($args) && $args | 84–91 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), add_filter(), add_filter(), do_action() |
$option_group === "misc" && $option_group === "privacy" | 89–102 | is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), add_filter(), do_action() |
6 further outcomes, up to 117 instructions
| always | 90–97 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), add_filter(), do_action() |
$option_group === "misc" && $option_group === "privacy" && !empty($args) && $args | 96–109 | is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), add_filter(), add_filter(), do_action() |
$option_group === "misc" && $option_group === "privacy" && empty($args) && !$args | 96–103 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), do_action() |
!empty($args) && $args | 97–104 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), add_filter(), add_filter(), do_action() |
$option_group === "misc" && $option_group === "privacy" | 103–110 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), add_filter(), do_action() |
$option_group === "misc" && $option_group === "privacy" && !empty($args) && $args | 110–117 | is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), add_filter(), add_filter(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 117 | 56–117 | 10 | |
| 8.5 | 117 | 56–117 | 10 | |
| 8.4 | 117 | 56–117 | 10 | |
| 8.3 | 117 | 56–117 | 10 | |
| 8.2 | 117 | 56–117 | 10 | |
| 8.1 | 117 | 56–117 | 10 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 118 | 57–118 | 10 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while register_setting() runs, in this order:
- apply_filters( register_setting_args )filterline 3031 (+35 into the body)
Filters the registration arguments when registering a setting.
- do_action( register_setting )actionline 3088 (+92 into the body)
Fires immediately before the setting is registered but after its filters are in place.
Uses · 7
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_parse_args()Merges user defined arguments into defaults array.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- add_filter()Adds a callback function to a filter hook.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 7
- Featured_Content::register_setting()Registers custom setting on the Settings -> Reading screen.
- _wp_register_default_connector_settings()Registers default connector settings.
- add_option_update_handler()Register a setting and its sanitization callback
- register_block_core_site_icon_setting()Register a core site setting for a site icon
- register_block_core_site_logo_setting()Register a core site setting for a site logo
- register_initial_settings()Registers default settings available in WordPress.
- twentyeleven_theme_options_init()Registers the form setting for our twentyeleven_options array.
Source code
function register_setting( $option_group, $option_name, $args = array() ) { global $new_allowed_options, $wp_registered_settings; /* * In 5.5.0, the `$new_whitelist_options` global variable was renamed to `$new_allowed_options`. * Please consider writing more inclusive code. */ $GLOBALS['new_whitelist_options'] = &$new_allowed_options; $defaults = array( 'type' => 'string', 'group' => $option_group, 'label' => '', '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 ); // Require an item schema when registering settings with an array type. if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) { _doing_it_wrong( __FUNCTION__, __( 'When registering an "array" setting to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.4.0' ); } if ( ! is_array( $wp_registered_settings ) ) { $wp_registered_settings = array(); } if ( 'misc' === $option_group ) { _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( /* translators: %s: misc */ __( '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( /* translators: %s: privacy */ __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) ); $option_group = 'reading'; } $new_allowed_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 ) ) {Changelog
Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
label argument.from the docblock$new_whitelist_options was renamed to $new_allowed_options.Please consider writing more inclusive code.from the docblock
$args can be passed to set flags on the setting, similar to register_meta().from the docblockprivacy option group was deprecated.from the docblockmisc option group was deprecated.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/option.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.