IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact
You are here: Home / CMS / WordPress / How to Fix PHP7 Compatibility Issue with W3 Total Cache

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

Comments

  1. yudha says

    October 10, 2016 at 9:30 pm

    after a few search, it just make me afraid to switch to php 7. I’m not good with code. so if w3tc still it’s not supported php7. I think i’ll wait 🙁

    thanks for the article, cause if i don’t read this, i don’t know what my site will look like.

    Reply
  2. Ulrich says

    September 7, 2016 at 11:59 pm

    Thank you so much! Now it works again!

    Reply
    • platt says

      September 10, 2016 at 2:33 pm

      You’re welcome Ulrich.

      Reply
  3. Mahammad Jan-E-Alam says

    September 2, 2016 at 7:13 pm

    Thanks….

    Reply
  4. Angel Kafaliev says

    August 4, 2016 at 6:58 am

    Great

    Reply
  5. Pavel Chmelar says

    July 28, 2016 at 6:41 pm

    Hi,

    thank you very much for this article.

    Note: I used PSPad and find & replace feature to search through plugin directory locally and then uploaded files online.

    Worked like a charm!

    Pavel

    Reply
    • platt says

      July 30, 2016 at 6:01 pm

      Glad to see it worked for you Pavel. Thanks for the tip as well.

      Cheers,
      platt

      Reply
  6. Serhii says

    July 21, 2016 at 7:58 pm

    Hello,
    please, help fix this for PHP 7.0.9

    https://wordpress.org/support/topic/w3-total-cache-and-php-709

    Thanks!

    Reply
    • platt says

      July 23, 2016 at 1:58 pm

      Hi Serhii,

      I saw that natenate19 already helped you solved the issue with 7.0.9. I’ve updated the post from there as well.

      Reply
  7. Anita Posch says

    July 15, 2016 at 7:37 am

    Perfect, thanks! But, will I stumble into the same problem after an upgrade of the W3 Total Cache Plugin??

    Reply
    • platt says

      July 23, 2016 at 1:59 pm

      Hi Anita,

      Unfortunately, this is a kludge and will be re-written with a new W3 Total Cache update. But rumor has it that it that the plugin will soon be updated with the permanent fix (I don’t know any dates though).

      Reply
  8. Manel says

    July 13, 2016 at 4:33 am

    Muchas gracias, un abrazo

    Reply
  9. Jamal says

    June 30, 2016 at 7:18 pm

    Thanks you for fixing this issue. Now my site is charming well

    Reply
  10. Kim K. says

    June 13, 2016 at 5:39 am

    You are awesome! Thanks so much!!

    Reply
  11. mrmlk says

    June 3, 2016 at 9:37 am

    amazing!!

    thank you so much

    Reply
  12. Optimator says

    May 14, 2016 at 6:48 am

    Thank you!! Perfect fix 🙂

    Reply
  13. no-memcache says

    May 7, 2016 at 11:55 pm

    Don’t forget that in many cases the the php class “Memcache” is not available and you will need to convert to “Memcached”. So you will also need to update W3TC to use “Memcached” instead of “Memcache”

    Reply
  14. Martin says

    April 10, 2016 at 12:44 am

    Thanks a bunch!
    Works fast and perfect!

    Reply
    • platt says

      April 13, 2016 at 8:30 pm

      You’re welcome Martin.

      Reply
  15. Name says

    March 22, 2016 at 12:25 am

    Worked charmly! Thanks

    Reply
  16. Pradeep M says

    March 21, 2016 at 10:16 pm

    Just wanted to say thanks. Worked well on WP v4.22 with W3TC V0.9.4.1.

    Reply
    • platt says

      March 22, 2016 at 5:53 pm

      You’re welcome Pradeep.

      Reply
  17. Odingk Prakoso says

    February 17, 2016 at 8:14 am

    thx… thank for your tutorial…

    Reply
    • platt says

      February 20, 2016 at 11:03 pm

      You’re very welcome Odingk.

      Reply
  18. Garv says

    February 6, 2016 at 1:04 am

    Yo Yo bro you saved my ton of work. Cause i was searching the issue in the given lines.

    Reply
  19. Laxmikant Bhumkar says

    February 1, 2016 at 5:09 am

    Thanks, itnota, for detecting very cumbersome edit of W3 Total Cache. We also unaware of its relation with PHP-7 update.

    Reply
    • platt says

      February 5, 2016 at 10:04 pm

      Thanks Laxmikant. If you saw one of my references, actually I wasn’t the one detecting and solved it. But I want to make sure more people don’t have to go through the headache having their system information is not broadcasted to the whole world after the upgrade.

      Reply

Leave a Reply Cancel reply

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

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
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to Check Installed .NET Framework Version
  • How to Remove .NET Runtime and SDK on Mac
  • How to Solve Intermittent 403 Error in IIS
  • How to Show Hidden Folders and Files in Mac Finder
  • How to Solve MS Office VBA Compile Error UserAuthentication

Recent Posts

  • How to Check Installed .NET Framework Version
  • How to Remove .NET Runtime and SDK on Mac
  • How to Solve Intermittent 403 Error in IIS
  • How to Show Hidden Folders and Files in Mac Finder
  • How to Solve MS Office VBA Compile Error UserAuthentication
  • RSS

Tags

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

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