I am used to register plugin settings like this:
$string_sanitization = array(
'sanitize_callback' => 'sanitize_text_field',
);
// Section Setting: Just some example string
register_setting(
'whatever', // Options group name
'some_example_string', // Option ID
$string_sanitization // Sanitization callback
);
And it works perfect.
But when it comes to options that store arrays of strings, I don't know what to use.
The docs contain a types
key for the args, but it's not clear enough, so I tried as follows:
register_setting(
'whatever', // Options group name
'array_of_strings', // Option ID
array(
'type' => 'array',
'sanitize_callback' => 'sanitize_text_field',
)
);
In this case the options don't get saved.
One thing that works is to create a custom sanitization function that handles the array, as follows:
function sanitize_array_of_strings( $array ) {
return array_map( 'sanitize_text_field', $array );
}
register_setting(
'whatever', // Options group name
'array_of_strings', // Option ID
array(
'type' => 'array',
'sanitize_callback' => 'My\Namespace\sanitize_array_of_strings',
)
);
But it seems strange to me that WP does not have a native way to do this, so I ask to find out if you know of a native way to achieve this without having to create a custom function for that.