I recently faced an interesting challenge: a site has one primary domain name and several secondary names. The site(s) have the same codebase, the same database, but several domain names. The site used an object cache drop-in. Site administrators used only the primary domain for posting. All the administrative operations triggered cache updates only for the primary domain; the mirrors on the secondary domains could show stale data. Site owners somehow needed to disable the object cache for all domains except the primary one.

Surprisingly, WordPress has a enable_loading_object_cache_dropin filter which controls whether to enable loading of the object-cache.php drop-in. That filter comes with one caveat, though:

This filter runs before it can be used by plugins. It is designed for non-web run-times. If false is returned, object-cache.php will never be loaded.

The above means that there is no way to write a plugin or a “must-use” plugin to control the loading of the drop-in.

However, there is a workaround (or a hack, if you will) to add a filter in your wp-config.php.

wp_debug_mode() function gives a hint how to do this:

* This filter runs before it can be used by plugins. It is designed for
* non-web run-times. Returning false causes the WP_DEBUG and related
* constants to not be checked and the default PHP values for errors
* will be used unless you take care to update them yourself.
*
* To use this filter you must define a $wp_filter global before
* WordPress loads, usually in wp-config.php.
*
* Example:
*
*     $GLOBALS['wp_filter'] = array(
*         'enable_wp_debug_mode_checks' => array(
*             10 => array(
*                 array(
*                     'accepted_args' => 0,
*                     'function'      => function() {
*                         return false;
*                     },
*                 ),
*             ),
*         ),
*     );

Of course, we can employ the same technique for the enable_loading_object_cache_dropin filter. All we need is to place this piece of code into wp-config.php above the require_once(ABSPATH . 'wp-settings.php'); line:

$GLOBALS['wp_filter'] = [
    'enable_loading_object_cache_dropin' => [
        10 => [
            [
                'accepted_args' => 1,
                'function'      => function ( $load ) {
                    /* Here comes the logic that determines whether WP should load object-cache.php */                
                    return $load;
                },
            ],
        ],
    ],
];
How to Conditionally Disable Object Cache Drop-in
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *