Using WordPress ‘add_allowed_options()’ PHP function

The add_allowed_options() WordPress PHP function adds an array of options to the list of allowed options in WordPress.

Usage

To use this function, simply call it with the required parameter, which is an array of new options. The second parameter is optional and can be a string or an array.

add_allowed_options(array('option1', 'option2'));

Parameters

  • $new_options (array): An array of options to be added. This is required.
  • $options (string|array): An existing option or array of options. This is optional and defaults to an empty string.

More Information

See WordPress Developer Resources: add_allowed_options()
The function is implemented in WordPress and has not been deprecated as of now. Check the WordPress Developer Resources link above for the latest information and source code location.

Examples

Adding Single Option

Here, we’re adding a single option named ‘theme_background_color’.

add_allowed_options(array('theme_background_color'));

Adding Multiple Options

In this example, we’re adding multiple options at once.

add_allowed_options(array('theme_background_color', 'theme_font_size', 'theme_layout_style'));

Adding an Option to an Existing Option

This is how you add new options to an existing option.

add_allowed_options(array('theme_background_color'), 'existing_option');

Adding Multiple Options to an Existing Array of Options

You can add multiple new options to an existing array of options like this.

add_allowed_options(array('theme_background_color', 'theme_font_size'), array('existing_option1', 'existing_option2'));

Adding an Option to Multiple Existing Options

Here, we’re adding a new option to multiple existing options.

add_allowed_options(array('theme_background_color'), array('existing_option1', 'existing_option2'));