Using WordPress ‘pre_wp_load_alloptions’ PHP filter

The pre_wp_load_alloptions WordPress PHP filter allows you to modify the array of alloptions before it is populated, or to bypass the default wp_load_alloptions() function.

Usage

add_filter('pre_wp_load_alloptions', 'your_custom_function', 10, 2);

function your_custom_function($alloptions, $force_cache) {
  // your custom code here
  return $alloptions;
}

Parameters

  • $alloptions: array|null – An array of alloptions. Default is null.
  • $force_cache: bool – Whether to force an update of the local cache from the persistent cache. Default is false.

More information

See WordPress Developer Resources: pre_wp_load_alloptions

Examples

Add an option to the alloptions array

add_filter('pre_wp_load_alloptions', 'add_my_custom_option', 10, 2);

function add_my_custom_option($alloptions, $force_cache) {
  $alloptions['my_custom_option'] = 'my_custom_value';
  return $alloptions;
}

Force cache update for alloptions

add_filter('pre_wp_load_alloptions', 'force_cache_update', 10, 2);

function force_cache_update($alloptions, $force_cache) {
  $force_cache = true;
  return $alloptions;
}

Remove an option from alloptions array

add_filter('pre_wp_load_alloptions', 'remove_my_custom_option', 10, 2);

function remove_my_custom_option($alloptions, $force_cache) {
  unset($alloptions['my_custom_option']);
  return $alloptions;
}

Modify the value of an option in alloptions array

add_filter('pre_wp_load_alloptions', 'modify_my_custom_option', 10, 2);

function modify_my_custom_option($alloptions, $force_cache) {
  if (isset($alloptions['my_custom_option'])) {
    $alloptions['my_custom_option'] = 'modified_custom_value';
  }
  return $alloptions;
}

Bypass wp_load_alloptions() and return a custom alloptions array

add_filter('pre_wp_load_alloptions', 'custom_alloptions_array', 10, 2);

function custom_alloptions_array($alloptions, $force_cache) {
  $custom_alloptions = array(
    'option1' => 'value1',
    'option2' => 'value2',
    'option3' => 'value3',
  );
  return $custom_alloptions;
}