The allowed_options WordPress PHP filter allows you to modify the list of allowed options in WordPress.
Usage
add_filter('allowed_options', 'your_custom_function');
function your_custom_function($allowed_options) {
// your custom code here
return $allowed_options;
}
Parameters
- $allowed_options (array): The original allowed options list.
More information
See WordPress Developer Resources: allowed_options
Examples
Add a custom option to the allowed options list
Add a custom option named ‘my_custom_option’ to the allowed options list.
add_filter('allowed_options', 'add_my_custom_option');
function add_my_custom_option($allowed_options) {
$allowed_options[] = 'my_custom_option';
return $allowed_options;
}
Remove an option from the allowed options list
Remove the ‘blogdescription’ option from the allowed options list.
add_filter('allowed_options', 'remove_blogdescription_option');
function remove_blogdescription_option($allowed_options) {
$key = array_search('blogdescription', $allowed_options);
if ($key !== false) {
unset($allowed_options[$key]);
}
return $allowed_options;
}
Restrict allowed options to a specific list
Restrict the allowed options to only ‘blogname’, ‘blogdescription’, and ‘admin_email’.
add_filter('allowed_options', 'restrict_allowed_options');
function restrict_allowed_options($allowed_options) {
return array('blogname', 'blogdescription', 'admin_email');
}
Sort allowed options alphabetically
Sort the allowed options list alphabetically.
add_filter('allowed_options', 'sort_allowed_options_alphabetically');
function sort_allowed_options_alphabetically($allowed_options) {
sort($allowed_options);
return $allowed_options;
}
Add a prefix to all allowed options
Add a ‘my_prefix_’ prefix to all allowed options.
add_filter('allowed_options', 'add_prefix_to_allowed_options');
function add_prefix_to_allowed_options($allowed_options) {
return array_map(function($option) {
return 'my_prefix_' . $option;
}, $allowed_options);
}