Using WordPress ‘pre_option_{$option}’ PHP filter

The pre_option_{$option} WordPress PHP filter allows you to modify the value of an existing option before it is retrieved. The dynamic part of the hook name, $option, refers to the option name. If the filter returns a value other than false, the retrieval process will be short-circuited, and the returned value will be used instead.

Usage

add_filter('pre_option_' . $option, 'your_custom_function', 10, 3);

function your_custom_function($pre_option, $option, $default_value) {
    // your custom code here
    return $pre_option;
}

Parameters

  • $pre_option (mixed): The value to return instead of the option value. This is different from $default_value, which is used as the fallback value if the option doesn’t exist elsewhere in get_option(). Default is false (to skip past the short-circuit).
  • $option (string): Option name.
  • $default_value (mixed): The fallback value to return if the option does not exist. Default is false.

More information

See WordPress Developer Resources: pre_option_{$option}

Examples

Changing site title

In this example, we’ll change the site title before it is retrieved.

add_filter('pre_option_blogname', 'change_site_title', 10, 3);

function change_site_title($pre_option, $option, $default_value) {
    $new_title = "New Site Title";
    return $new_title;
}

Change admin email

In this example, we’ll change the admin email before it is retrieved.

add_filter('pre_option_admin_email', 'change_admin_email', 10, 3);

function change_admin_email($pre_option, $option, $default_value) {
    $new_email = "[email protected]";
    return $new_email;
}

Change timezone

In this example, we’ll change the timezone before it is retrieved.

add_filter('pre_option_timezone_string', 'change_timezone', 10, 3);

function change_timezone($pre_option, $option, $default_value) {
    $new_timezone = "America/New_York";
    return $new_timezone;
}

Change date format

In this example, we’ll change the date format before it is retrieved.

add_filter('pre_option_date_format', 'change_date_format', 10, 3);

function change_date_format($pre_option, $option, $default_value) {
    $new_format = "d/m/Y";
    return $new_format;
}

Change posts per page

In this example, we’ll change the number of posts displayed per page before it is retrieved.

add_filter('pre_option_posts_per_page', 'change_posts_per_page', 10, 3);

function change_posts_per_page($pre_option, $option, $default_value) {
    $new_posts_per_page = 12;
    return $new_posts_per_page;
}