wppaste
WordPress

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
Registers a setting and its data.

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()
  • $typestring

    The type of data associated with this setting. Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  • $labelstring

    A label of the data attached to this setting.
  • $descriptionstring

    A description of the data attached to this setting.
  • $sanitize_callbackcallable

    A callback function that sanitizes the option's value.
  • $show_in_restbool|array

    Whether 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
56–117

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 117.

Plugin surface
2 hooks

Third-party callbacks on 'register_setting_args', 'register_setting' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
$option_group !== "misc" && $option_group !== "privacy" && empty($args) && !$args56–69is_callable(), apply_filters(), wp_parse_args(), do_action()
$option_group !== "misc" && $option_group !== "privacy"63–76is_callable(), apply_filters(), wp_parse_args(), add_filter(), do_action()
empty($args) && !$args69–82is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), do_action()
$option_group !== "misc" && $option_group !== "privacy" && !empty($args) && $args70–83is_callable(), apply_filters(), wp_parse_args(), add_filter(), add_filter(), do_action()
$option_group !== "misc" && $option_group !== "privacy" && empty($args) && !$args70–77is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), do_action()
always76–89is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), add_filter(), do_action()
$option_group !== "misc" && $option_group !== "privacy"77–84is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), add_filter(), do_action()
$option_group === "misc" && $option_group === "privacy" && empty($args) && !$args82–95is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), do_action()
!empty($args) && $args83–96is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), add_filter(), add_filter(), do_action()
empty($args) && !$args83–90is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), do_action()
$option_group !== "misc" && $option_group !== "privacy" && !empty($args) && $args84–91is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), add_filter(), add_filter(), do_action()
$option_group === "misc" && $option_group === "privacy"89–102is_callable(), apply_filters(), wp_parse_args(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), add_filter(), do_action()
6 further outcomes, up to 117 instructions
always90–97is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), add_filter(), do_action()
$option_group === "misc" && $option_group === "privacy" && !empty($args) && $args96–109is_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) && !$args96–103is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), do_action()
!empty($args) && $args97–104is_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–110is_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) && $args110–117is_callable(), apply_filters(), wp_parse_args(), __(), _doing_it_wrong(), __(), sprintf(), _deprecated_argument(), __(), sprintf(), _deprecated_argument(), add_filter(), add_filter(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11756–11710
8.511756–11710
8.411756–11710
8.311756–11710
8.211756–11710
8.111756–117101 fewer instruction than PHP 7.4
7.411857–11810

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:

  1. apply_filters( register_setting_args )filterline 3031 (+35 into the body)

    Filters the registration arguments when registering a setting.

  2. 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

Used by · 7

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.6.0
Added the label argument.from the docblock
5.5.0
$new_whitelist_options was renamed to $new_allowed_options.
Please consider writing more inclusive code.from the docblock
4.7.0
$args can be passed to set flags on the setting, similar to register_meta().from the docblock
3.5.0
The privacy option group was deprecated.from the docblock
3.0.0
The misc option group was deprecated.from the docblock
2.7.0
Introduced.from the docblock

About 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.