Using WordPress ‘register_initial_settings()’ PHP function

The register_initial_settings() WordPress PHP function registers default settings available in WordPress, primarily for use with the REST API.

Usage

To use the register_initial_settings() function, simply call it in your theme or plugin file:

register_initial_settings();

Parameters

  • None

More information

See WordPress Developer Resources: register_initial_settings

This function is mostly used internally by WordPress for REST API purposes.

Examples

Registering initial settings in a plugin

When developing a plugin that uses WordPress settings, you can call register_initial_settings() to ensure that the default settings are available:

function my_plugin_init() {
  register_initial_settings();
}

add_action('init', 'my_plugin_init');

Registering initial settings in a custom REST API route

To ensure that the default settings are available when using a custom REST API route:

function my_custom_route_callback() {
  register_initial_settings();
  // Your custom route logic here
}

add_action('rest_api_init', function () {
  register_rest_route('my-namespace/v1', '/my-route', array(
    'methods' => 'GET',
    'callback' => 'my_custom_route_callback',
  ));
});

Registering initial settings in a theme

If you’re developing a theme that uses WordPress settings, call register_initial_settings() in your theme’s functions.php file:

function my_theme_init() {
  register_initial_settings();
}

add_action('init', 'my_theme_init');

Registering initial settings before using get_option()

When using the get_option() function in your plugin or theme, ensure that the default settings are registered:

register_initial_settings();
$option = get_option('my_option_name');

Registering initial settings before using update_option()

Before updating an option using the update_option() function, make sure the default settings are registered:

register_initial_settings();
update_option('my_option_name', 'my_option_value');