The ‘register_setting_args’ filter allows you to modify the registration arguments when registering a setting in WordPress.
Usage
To use this filter, add a custom function to your theme’s functions.php file or a custom plugin. Then, hook that function to the register_setting_args filter using add_filter().
Code example:
function my_custom_register_setting_args( $args, $defaults, $option_group, $option_name ) {
// Your custom code here
}
add_filter( 'register_setting_args', 'my_custom_register_setting_args', 10, 4 );
Parameters
- $args (array): Array of setting registration arguments.
- $defaults (array): Array of default arguments.
- $option_group (string): Setting group.
- $option_name (string): Setting name.
Examples
Modify default sanitize_callback
Scenario: You want to use a custom sanitization function for all registered settings.
Code example:
function my_custom_sanitize_callback( $value, $args ) {
// Your custom sanitization logic
return $sanitized_value;
}
function change_default_sanitize_callback( $args, $defaults, $option_group, $option_name ) {
$args['sanitize_callback'] = 'my_custom_sanitize_callback';
return $args;
}
add_filter( 'register_setting_args', 'change_default_sanitize_callback', 10, 4 );
Explanation: This code defines a custom sanitization function (my_custom_sanitize_callback) and sets it as the default sanitize_callback for all registered settings by modifying the $args array.
Add a custom validation function
Scenario: You want to add a custom validation function for a specific setting.
Code example:
function my_custom_validation( $value, $args ) {
// Your custom validation logic
return $is_valid;
}
function add_custom_validation( $args, $defaults, $option_group, $option_name ) {
if ( 'my_custom_setting' === $option_name ) {
$args['validate_callback'] = 'my_custom_validation';
}
return $args;
}
add_filter( 'register_setting_args', 'add_custom_validation', 10, 4 );
Explanation: This code defines a custom validation function (my_custom_validation) and adds it as the validate_callback for the setting named my_custom_setting.
Make all settings autoload
Scenario: You want all registered settings to autoload by default.
Code example:
function make_settings_autoload( $args, $defaults, $option_group, $option_name ) {
$args['autoload'] = true;
return $args;
}
add_filter( 'register_setting_args', 'make_settings_autoload', 10, 4 );
Explanation: This code sets the autoload property to true for all registered settings, making them autoload by default.
Change default show_in_rest
Scenario: You want all registered settings to be accessible via the REST API by default.
Code example:
function enable_show_in_rest_by_default( $args, $defaults, $option_group, $option_name ) {
$args['show_in_rest'] = true;
return $args;
}
add_filter( 'register_setting_args', 'enable_show_in_rest_by_default', 10, 4 );
Explanation: This code sets the show_in_rest property to true for all registered settings, making them accessible via the REST API by default.
Add a custom description for a specific setting
Scenario: You want to add a custom description for a specific setting in your WordPress options.
Code example:
function add_custom_description( $args, $defaults, $option_group, $option_name ) {
if ( 'my_specific_setting' === $option_name ) {
$args['description'] = 'This is a custom description for my specific setting.';
}
return $args;
}
add_filter( 'register_setting_args', 'add_custom_description', 10, 4 );
Explanation: This code checks if the current setting’s name is my_specific_setting. If it is, the description property in the $args array is updated with a custom description.