Tech blog

WP Hive + W3 Total Cache

wp-hive_and_w3-total-cache

Recently I have discovered great caching plugin for WordPress: W3 Total Cache. I’m not going to write how to install and use it (well maybe I will in some other post later but there’s a lot of informations on the web about it) but I’ll tell you one thing: this plugin is much more powerful than very popular and commonly used WP Super Cache and you should at least try it. There is one problem with W3 Total Cache (W3TC) though: it’s not compatible with WP Hive plugin so if you’re using WP Hive you’ll have to hack W3TC and I’ll show you how I did it.

One important note: solution described here will only work for multiple blogs installed in subdomains, it’ll probably not work for those installed in subdirectories (but I did not tested that).

OK, let’s start.
Both Wp Hive and W3 Total Cache are using wp-content/db.php file and they both overwrite this file during installation (it is not WordPress core file). So when you install W3TC it’ll break WP Hive by overwriting wp-content/db.php with its own content. Solution is simple: you have to merge code needed by WP Hive and W3TC into one wp-content/db.php file. You’ll find source files for merge operation in wp-content/plugins/wp-hive/db.php and wp-content/plugins/w3-total-cache/wp-content/db.php. Do not concatenate those files because you may get some strange errors (well at least I had). So do not create something like this:

<?php
/*
 WP Hive...
 */
...
?>

<?php
/**
 * W3 Total Cache Database module
 */
...

but better something like that:

<?php
/*
 WP Hive...
 */
...


/**
 * W3 Total Cache Database module
 */
...

?>

Difference is simple: closing PHP tag was added at the end, closing and opening PHP tags between plugins code were removed.

OK, so now both plugins are properly installed but W3TC needs to know that we have multiblogging feature on our installation. Thankfully it has support for WPMU and WordPress 3 “network of sites” new feature. We will just modify two functions used by W3TC to detect if it’s in multisite WordPress environment. We’ll be editing wp-content/plugins/w3-total-cache/inc/define.php file. Find this portion of code (it’s somewhere around line 255):

/**
 * Returns if there is multisite mode
 *
 * @return boolean
 */
function w3_is_multisite()
{
    return (w3_is_wpmu() || w3_is_network_mode());
}

/**
 * Returns true if WPMU uses vhosts
 *
 * @return boolean
 */
function w3_is_vhost()
{
    return ((defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) || (defined('VHOST') && VHOST == 'yes'));
}

And change it into that:

/**
 * Returns if there is multisite mode
 *
 * @return boolean
 */
function w3_is_multisite()
{
    // WP Hive support hack
    return true;
    return (w3_is_wpmu() || w3_is_network_mode());
}

/**
 * Returns true if WPMU uses vhosts
 *
 * @return boolean
 */
function w3_is_vhost()
{
    // WP Hive support hack
    return true;
    return ((defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) || (defined('VHOST') && VHOST == 'yes'));
}

That’s all folks! :) Simple isn’t it?

W3 Total Cache should now create separate caching folders for each WP Hive powered blog (but not for those in subdirectories). Of course you have to enable and configure W3TC for each blog separately.

This entry was posted on 30 August 2010 at 02:26 in category WordPress.

  1. 6 December 2010 at 22:43
    Pete says:

    Will the same hack work for websites with their own domain using hive?

    1. 6 December 2010 at 22:52
      barszcz says:

      I think it should work but I did not test that. I see no reason why it should not work :)

  2. 6 December 2010 at 23:12
    Pete says:

    Sounds great!

    I’ll do a little checking in the next days and let you know if it works. :-)

    1. 6 December 2010 at 23:17
      barszcz says:

      Thanks, I’d be grateful.

  3. 7 December 2010 at 05:45

    I am using wp-hive with domain mode – Your instruction ok until I add “return true;” . It make whole domain blank (wp. 3.02)

    1. 7 December 2010 at 12:40
      barszcz says:

      Hmm… that’s strange. I’ll take a look at this and make some tests in some spare time.

  4. 31 July 2011 at 19:47
    Andrew says:

    Have you looked at this recently? I’ve been trying to get WP-Hive 0.6.2 and W3 Total Cache 0.9.2.3 to play nicely together, but the code in the current version of the define.php file has changed since you wrote this and I’m not sure what changes to make to it.

    1. 31 July 2011 at 21:59
      barszcz says:

      No. I’ve reinstalled WordPress and now I’m using built-in network of sites feature so I’m not using WP-Hive anymore.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>