Using WordPress ‘pre_update_option’ PHP filter

‘pre_update_option’ is a WordPress PHP filter that allows you to modify an option’s value before it is potentially serialized and updated in the database.

Usage

To use this filter, you need to create a custom function that will be hooked to the pre_update_option filter. Here’s a code example:

function my_custom_pre_update_option( $value, $option, $old_value ) {
    // Your custom code here
    return $value;
}
add_filter( 'pre_update_option', 'my_custom_pre_update_option', 10, 3 );

Parameters

  • $value (mixed)
    • The new, unserialized option value.
  • $option (string)
    • Name of the option.
  • $old_value (mixed)
    • The old option value.

Examples

Sanitizing input before saving

function sanitize_my_option( $value, $option, $old_value ) {
    if ( 'my_option' === $option ) {
        return sanitize_text_field( $value );
    }
    return $value;
}
add_filter( 'pre_update_option', 'sanitize_my_option', 10, 3 );

In this example, the custom function sanitize_my_option checks if the option being updated is my_option. If it is, it sanitizes the new value using the sanitize_text_field() function before saving it.

Preventing an option from being updated

function prevent_option_update( $value, $option, $old_value ) {
    if ( 'protected_option' === $option ) {
        return $old_value;
    }
    return $value;
}
add_filter( 'pre_update_option', 'prevent_option_update', 10, 3 );

In this example, the prevent_option_update function checks if the option being updated is protected_option. If it is, the old value is returned instead, effectively preventing the option from being updated.

Converting an option value to uppercase

function uppercase_option_value( $value, $option, $old_value ) {
    if ( 'uppercase_option' === $option ) {
        return strtoupper( $value );
    }
    return $value;
}
add_filter( 'pre_update_option', 'uppercase_option_value', 10, 3 );

In this example, the uppercase_option_value function checks if the option being updated is uppercase_option. If it is, the new value is converted to uppercase using the strtoupper() function before saving.

Setting a minimum value for a numeric option

function set_min_value( $value, $option, $old_value ) {
    if ( 'min_value_option' === $option && $value < 10 ) {
        return 10;
    }
    return $value;
}
add_filter( 'pre_update_option', 'set_min_value', 10, 3 );

In this example, the set_min_value function checks if the option being updated is min_value_option. If it is and the new value is less than 10, the value is set to 10 before saving.

Transforming an option value to a JSON string

function transform_to_json( $value, $option, $old_value ) {
    if ( 'json_option' === $option ) {
        return json_encode( $value );
    }
    return $value;
}
add_filter( 'pre_update_option', 'transform_to_json', 10, 3 );

In this example, the `transform_to_json` function checks if the option being updated is `json_option`. If it is, the new value is transformed into a JSON string using the `json_encode()` function before saving.