Using WordPress ‘register_setting’ PHP action

The register_setting WordPress PHP action is used to register a setting with a specific option group and name, along with additional arguments.

Usage

add_action('register_setting', 'your_custom_function', 10, 3);

function your_custom_function($option_group, $option_name, $args) {
  // Your custom code here

  return $option_group;
}

Parameters

  • $option_group (string): The setting group.
  • $option_name (string): The setting name.
  • $args (array): An array of setting registration arguments.

More information

See WordPress Developer Resources: register_setting

Examples

Log registered settings

Log the registered settings to a debug log.

add_action('register_setting', 'log_registered_settings', 10, 3);

function log_registered_settings($option_group, $option_name, $args) {
  error_log("Setting registered: Group: {$option_group}, Name: {$option_name}, Args: " . print_r($args, true));
  return $option_group;
}

Modify registered setting arguments

Change the sanitize_callback for registered settings.

add_action('register_setting', 'modify_registered_setting_arguments', 10, 3);

function modify_registered_setting_arguments($option_group, $option_name, $args) {
  if ('your_setting_group' === $option_group) {
    $args['sanitize_callback'] = 'your_custom_sanitize_callback';
  }
  return $option_group;
}

Add a custom capability for a registered setting

Add a custom capability for a specific setting.

add_action('register_setting', 'add_custom_capability_for_setting', 10, 3);

function add_custom_capability_for_setting($option_group, $option_name, $args) {
  if ('your_setting_name' === $option_name) {
    $args['capability'] = 'your_custom_capability';
  }
  return $option_group;
}

Register additional settings based on registered settings

Register an additional setting based on the existence of a specific setting.

add_action('register_setting', 'register_additional_settings', 10, 3);

function register_additional_settings($option_group, $option_name, $args) {
  if ('your_setting_name' === $option_name) {
    register_setting($option_group, 'your_additional_setting', $args);
  }
  return $option_group;
}

Modify setting value upon registration

Modify the value of a setting when it is registered.

add_action('register_setting', 'modify_setting_value_on_registration', 10, 3);

function modify_setting_value_on_registration($option_group, $option_name, $args) {
  if ('your_setting_name' === $option_name) {
    $current_value = get_option($option_name);
    $modified_value = strtoupper($current_value);
    update_option($option_name, $modified_value);
  }
  return $option_group;
}