Using WordPress ‘enable_loading_object_cache_dropin’ PHP filter

The enable_loading_object_cache_dropin WordPress PHP filter allows you to control if the object-cache.php drop-in should be loaded or not, typically used for non-web runtimes.

Usage

add_filter('enable_loading_object_cache_dropin', 'my_custom_function');
function my_custom_function($enable_object_cache) {
    // Your custom code here
    return $enable_object_cache;
}

Parameters

  • $enable_object_cache (bool): Determines whether to enable loading object-cache.php (if present). Default is true.

More information

See WordPress Developer Resources: enable_loading_object_cache_dropin

Examples

Disable Object Cache for CLI Commands

Disable the object cache when running WP-CLI commands.

add_filter('enable_loading_object_cache_dropin', 'disable_object_cache_for_cli');
function disable_object_cache_for_cli($enable_object_cache) {
    if (defined('WP_CLI') && WP_CLI) {
        return false;
    }
    return $enable_object_cache;
}

Disable Object Cache for Cron Jobs

Disable the object cache when running WP-Cron jobs.

add_filter('enable_loading_object_cache_dropin', 'disable_object_cache_for_cron');
function disable_object_cache_for_cron($enable_object_cache) {
    if (defined('DOING_CRON') && DOING_CRON) {
        return false;
    }
    return $enable_object_cache;
}

Disable Object Cache for AJAX Requests

Disable the object cache for AJAX requests.

add_filter('enable_loading_object_cache_dropin', 'disable_object_cache_for_ajax');
function disable_object_cache_for_ajax($enable_object_cache) {
    if (defined('DOING_AJAX') && DOING_AJAX) {
        return false;
    }
    return $enable_object_cache;
}

Disable Object Cache for REST API Requests

Disable the object cache for REST API requests.

add_filter('enable_loading_object_cache_dropin', 'disable_object_cache_for_rest');
function disable_object_cache_for_rest($enable_object_cache) {
    if (defined('REST_REQUEST') && REST_REQUEST) {
        return false;
    }
    return $enable_object_cache;
}

Disable Object Cache for Custom Condition

Disable the object cache based on a custom condition, e.g., a query parameter.

add_filter('enable_loading_object_cache_dropin', 'disable_object_cache_for_custom_condition');
function disable_object_cache_for_custom_condition($enable_object_cache) {
    if (isset($_GET['disable_object_cache']) && $_GET['disable_object_cache'] === 'true') {
        return false;
    }
    return $enable_object_cache;
}