IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact

How to Fix PHP7 Compatibility Issue with W3 Total Cache

With the advent of PHP 7, a few webhosts such as SiteGround have made it available on all their shared hosting services and you can find a way to enable it here.

Before you jump the bandwagon though, be aware of the warning they posted on their website:

Please have in mind that even if you’re using an app that supports PHP 7 some of its components like plugins, themes, modules, etc. may have old code that will not work on the latest version. This is why we advise that you always test on a staging environment before you enable it on your live site.

You’ll run into a compatibility issue right away if you use one of the WordPress plugins, W3 Total Cache (W3TC). Right after upgrading to PHP 7, I saw this message on the bottom of every page of my WordPress blog:

Warning: Parameter 1 to W3_Plugin_TotalCache::ob_callback() expected to be a reference, value given in WP_PATH/wp-includes/functions.php on line 3297

Actually this error message is misleading, because the real issue is not in functions.php file on line 3297. Instead, you need to go to W3 Total Cache directory within your WordPress installation under wp-content folder and look for a file called TotalCache.php.

The whole path would be:
WP_PATH\wp-content\plugins\w3-total-cache\lib\W3\Plugin\TotalCache.php

Look for line 512 (highlighted):

    /**
     * Output buffering callback
     *
     * @param string $buffer
     * @return string
     */
    function ob_callback(&$buffer) {
        global $wpdb;

Remove the ampersand “&” from the code as the following:

    function ob_callback($buffer) {
        global $wpdb;

Here’s the before and after comparison:
W3 TotalCache.php modification for PHP7 compatibility

Once you’re done changing the code, just clear all the caching and reload your page. This time the error message should disappear.

Update for PHP 7.0.9

With the release of PHP 7.0.9, some people have already encountered more issues with this W3 Total Cache plugin after upgrading.

Typical error messages:

Warning: Parameter 1 to W3_Plugin_Minify::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_Plugin_Cdn::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_PgCache::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_Plugin_Minify::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_Plugin_Cdn::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_PgCache::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_Plugin_Minify::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_Plugin_Cdn::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23
Warning: Parameter 1 to W3_PgCache::ob_callback() expected to be a reference, value given in /wp-content/plugins/w3-total-cache/inc/functions/plugin.php on line 23

Fortunately someone from WordPress forum has already posted a good solution. I personally have not tested this, but at least others have had success implementing the fix.

Just to sum up, you basically need to change make the same code modification as above, but to more files.

If you use Git, you can easily find all these files by running this command:

$ git grep -e 'ob_callback(&$buffer)' -n

W3 Total Cache ob_callback

And it should list all the filenames you need to update.

WP_PATH\wp-content\plugins\w3-total-cache\lib\W3\PgCache.php
WP_PATH\wp-content\plugins\w3-total-cache\lib\W3\Plugin\BrowserCache.php
WP_PATH\wp-content\plugins\w3-total-cache\lib\W3\Plugin\Cdn.php
WP_PATH\wp-content\plugins\w3-total-cache\lib\W3\Plugin\Minify.php
WP_PATH\wp-content\plugins\w3-total-cache\lib\W3\Plugin\NewRelic.php
WP_PATH\wp-content\plugins\w3-total-cache\lib\W3\Plugin\TotalCache.php // *

* You will only see the last file if you haven’t fixed it from the earlier instruction above.

Update each file so that all occurrences of ob_callback(&$buffer) are replaced with ob_callback($buffer).

Finally, it’s worth repeating, backup before you do anything.

If you rather not deal with all these operations yourself, you may want to use some of the fixes and customization like this one (use at your own risk).

References

Modern PHP: New Features and Good Practices
Learning PHP 7: A Pain-Free Introduction to Building Interactive Web Sites
PHP for the Web: Visual QuickStart Guide (5th Edition)
Fixing Warning on HHVM
W3 Total Cache and PHP 7.0.9
Fix and customize W3 Total Cache

December 22, 2015 Filed Under: WordPress Tagged With: PHP

Buy me a coffee?

Buy me a coffee If you find any of the articles or demos helpful, please consider supporting my work here, you'll have my big thanks!

Buy me a coffee Support this site
Buy Me a Coffee?

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • Tips and Tricks
  • Uncategorized
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Tags

.NET .NET Core AdSense ASP.NET Cdonts Dll Classic ASP Code Editor ETL FSharp Genesis Framework Git Google HP Asset Manager HTML5 Hugo IIS Information Security Internet Internet Information Services iOS JAMStack Linux macOS Microsoft Microsoft SQL Server MVC PHP PowerShell Python Simple Mail Transfer Protocol Smtp Server SQL SQL Server SSIS SSMS SSRS Sublime Text Visual Studio Visual Studio Code VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

Copyright © 2011-2025 IT Nota. All rights reserved. Terms of Use | Privacy Policy | Disclosure