Using WordPress ‘pre_update_site_option_{$option}’ PHP filter

‘pre_update_site_option_{$option}’ is a dynamic WordPress PHP filter that  allows you to filter a specific network option before its value is updated in WordPress.

The dynamic part of the hook name ({$option}) refers to the option name.

Usage

function my_pre_update_site_option_option( $value, $old_value, $option, $network_id ) {
    return $value;
}
add_filters( "pre_update_site_option_{$option}", "my_pre_update_site_option_option" );

Parameters

  • $value (mixed): New value of the network option.
  • $old_value (mixed): Old value of the network option.
  • $option (string): Option name.
  • $network_id (int): ID of the network.

Examples

Updating Maximum File Upload Size

function update_max_upload_size($value, $old_value, $option, $network_id) {
    // Set the new maximum upload size to 10MB
    return 10 * 1024 * 1024;
}
add_filter('pre_update_site_option_max_upload_size', 'update_max_upload_size', 10, 4);

In this example, we use the pre_update_site_option_max_upload_size filter to update the maximum file upload size to 10MB.

Enforcing a Minimum Site Title Length

function enforce_min_site_title_length($value, $old_value, $option, $network_id) {
    if (strlen($value) < 5) {
        return $old_value;
    }
    return $value;
}
add_filter('pre_update_site_option_blogname', 'enforce_min_site_title_length', 10, 4);

This code enforces a minimum site title length of 5 characters. If the new value is shorter than 5 characters, the old value is returned instead.

Disallowing Certain Characters in Site Descriptions

function disallow_chars_in_site_description($value, $old_value, $option, $network_id) {
    return preg_replace('/[^a-zA-Z0-9\s]/', '', $value);
}
add_filter('pre_update_site_option_blogdescription', 'disallow_chars_in_site_description', 10, 4);

This example removes any non-alphanumeric characters from the site description.

Updating Default Role for New Users

function update_default_role($value, $old_value, $option, $network_id) {
    // Set the new default role to 'editor'
    return 'editor';
}
add_filter('pre_update_site_option_default_role', 'update_default_role', 10, 4);

In this scenario, we update the default role for new users to ‘editor’.

Disallowing Changes to the Admin Email

function disallow_admin_email_changes($value, $old_value, $option, $network_id) {
    return $old_value;
}
add_filter('pre_update_site_option_admin_email', 'disallow_admin_email_changes', 10, 4);

This code prevents changes to the admin email by always returning the old value.