Using WordPress ‘option_update_filter()’ PHP function

The option_update_filter() WordPress PHP function refreshes the value of the allowed options list available via the ‘allowed_options’ hook.

Usage

option_update_filter( $options );

Parameters

  • $options (array) – Required. The list of allowed options to be refreshed.

More information

See WordPress Developer Resources: option_update_filter

Examples

Refresh allowed options list

In this example, we use the option_update_filter() function to refresh the allowed options list.

// Register a custom option to the allowed options list
function my_custom_option( $allowed_options ) {
    $allowed_options['my_option'] = 'my_option_value';
    return $allowed_options;
}
add_filter( 'allowed_options', 'my_custom_option' );

// Refresh the allowed options list
$allowed_options = apply_filters( 'allowed_options', array() );
option_update_filter( $allowed_options );

Modify allowed options and refresh

In this example, we modify the allowed options list and use the option_update_filter() function to refresh the list.

// Function to modify allowed options list
function modify_allowed_options( $allowed_options ) {
    $allowed_options['new_option'] = 'new_option_value';
    unset($allowed_options['old_option']);
    return $allowed_options;
}

// Get the current allowed options list
$allowed_options = apply_filters( 'allowed_options', array() );

// Modify the allowed options list
$modified_options = modify_allowed_options( $allowed_options );

// Refresh the allowed options list
option_update_filter( $modified_options );

Refresh allowed options list on a specific action

In this example, we use the option_update_filter() function to refresh the allowed options list when a specific action is triggered.

// Refresh allowed options on 'my_custom_action'
function refresh_allowed_options_on_action() {
    $allowed_options = apply_filters( 'allowed_options', array() );
    option_update_filter( $allowed_options );
}
add_action( 'my_custom_action', 'refresh_allowed_options_on_action' );

Add multiple options and refresh

In this example, we add multiple options to the allowed options list and use the option_update_filter() function to refresh the list.

// Function to add multiple options
function add_multiple_options( $allowed_options ) {
    $allowed_options['option_one'] = 'option_one_value';
    $allowed_options['option_two'] = 'option_two_value';
    $allowed_options['option_three'] = 'option_three_value';
    return $allowed_options;
}

// Get the current allowed options list
$allowed_options = apply_filters( 'allowed_options', array() );

// Add multiple options to the allowed options list
$new_options = add_multiple_options( $allowed_options );

// Refresh the allowed options list
option_update_filter( $new_options );

Remove an option and refresh

In this example, we remove an option from the allowed options list and use the option_update_filter() function to refresh the list.

// Function to remove an option
function remove_option( $allowed_options, $option_to_remove ) {
    unset( $allowed_options[$option_to_remove] );
    return $allowed_options;
}

// Get the current allowed options list
$allowed_options = apply_filters( 'allowed_options', array() );

// Remove an option from the allowed options list
$updated_options = remove_option( $allowed_options, 'option_to_remove' );

// Refresh