Using WordPress ‘add_option_whitelist()’ PHP function

The add_option_whitelist() WordPress PHP function is utilized to append an array of options to the set of permitted options.

Usage

Let’s imagine you have an array of options, $my_options, and you want to add these to the WordPress whitelist. Here’s how you’d do it:

$new_options = array('option1', 'option2', 'option3');
add_option_whitelist($new_options);

This code adds 'option1', 'option2', and 'option3' to the whitelist.

Parameters

  • $new_options (array): The array of new options that you want to add to the whitelist.
  • $options (string|array): This is optional. It can either be a string or an array. Default is an empty string.

More information

See WordPress Developer Resources: add_option_whitelist

Examples

Add a single option to the whitelist

This code will add 'my_option' to the list of allowed options.

$new_option = array('my_option');
add_option_whitelist($new_option);

Add multiple options to the whitelist

This code will add 'option_a', 'option_b', and 'option_c' to the list of allowed options.

$new_options = array('option_a', 'option_b', 'option_c');
add_option_whitelist($new_options);

Add an array of options to the whitelist

This code will add an array of options to the list of allowed options.

$new_options = array('option1', 'option2', 'option3');
add_option_whitelist($new_options);

Using the optional parameter

In this example, the optional $options parameter is used to specify a second list of options.

$new_options = array('option1', 'option2', 'option3');
$additional_options = array('option4', 'option5');
add_option_whitelist($new_options, $additional_options);

Adding options from a function

This example shows how to add options from a function.

function get_my_options() {
    return array('option1', 'option2', 'option3');
}

$new_options = get_my_options();
add_option_whitelist($new_options);

In this code, the get_my_options function returns an array of options. This array is then added to the whitelist using add_option_whitelist.