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

How to Add ID Attribute in Genesis Main Content

With the upgrade of Genesis Framework to responsive design, there’s a particular id attribute on the web content that’s deprecated when you use HTML5. If you take a look at /genesis/lib/framework.php file from Genesis core, you will see that the id="content" markup is still included in the XHTML markup. However for HTML5, the ID attritube is no longer there.

genesis_markup( array(
     'html5'   => '<main %s>',
     'xhtml'   => '<div id="content" class="hfeed">',
     'context' => 'content',
) );

This change creates a problem for some custom javascript to make Google AdSense responsive. The easiest thing to do is just to modify framework.php file to look like this:

genesis_markup( array(
     'html5'   => '<main id="content" %s>',
     'xhtml'   => '<div id="content" class="hfeed">',
     'context' => 'content',
) );

But obviously this is the worst thing to do because any future updates on Genesis core will overwrite that modification. So we want to make the change on the Genesis child theme instead.

Fortunately Genesis markup is very easy to modify. We just need to apply filter within the context of any section we want to change. In this case, it’s the content and we just need to do this on the Genesis child theme functions.php file.

// Add id="content" attributes to <main> element
add_filter( 'genesis_attr_content', 'my_attr_content' );
function my_attr_content( $attr ) {

     $attr['id'] .= 'content';
     return $attr;
    
}

The flexibility of filter in Genesis Framework is not limited to what’s shown in this example. You can also create custom schema for anything on your site by using the same method.

Further Reading

Custom microdata with Genesis 2.0

July 5, 2014 Filed Under: How To, WordPress Tagged With: Genesis Framework

How to Change WordPress Upload Folder

If you do a fresh install using WordPress 3.5 or later, you may have noticed that one useful feature that’s sorely missing is the ability to change the default upload directory from the Administration page.

WordPress Old Media Settings (Administration Dashboard)

Not sure why it was taken out in the first place, fortunately there’s still a way to do it without too much trouble.

You just need to add the following code in the wp-config.php file:

// Change default upload directory to '/myimages'
define( 'UPLOADS', 'myimages' );

The example above is based on the assumption that you want your upload directory in a sub-folder called ‘/myimages’ at the root of your WordPress installation. If you prefer to do it within your wp-content folder, make sure you include the path in the prefix:

// Change default upload directory to '/wp-content/myimages'
define( 'UPLOADS', 'wp-content/myimages' );

Note: The above code needs to be added before the last section of wp-config.php that says:

/** Sets up WordPress vars and included files. */
require_once( ABSPATH . 'wp-settings.php' );

Further Reading

Editing wp-config.php: Moving Upload Folders

February 17, 2014 Filed Under: WordPress Tagged With: PHP

Migrating Genesis Metadata to Yoast SEO

If you install Yoast’s WordPress SEO Plugin on top of your Genesis theme, you’ll notice that all your manually typed meta descriptions (and maybe keywords) just went completely missing. Meta description is an important element for search engine to index your pages and when you already have more than hundreds of pages and posts or even ten, then re-typing is definitely not an option.

Since I’m not aware of any way to do this from the plugin, the easiest way to do this is on the database.

Again as a forewarning, before you attempt to do this, backup everything in your MySQL to make sure you can revert back if you make a mistake. The solution is quick and simple, but it’s so easy to make a mistake that will corrupt your data and possibly more.

Go to your SQL admin interface such as phpMyAdmin (or you can do command line interface if you prefer) and run these sql commands:

UPDATE wp_postmeta
SET meta_key = '_yoast_wpseo_metadesc'
WHERE meta_key = '_genesis_description';

UPDATE wp_postmeta
SET meta_key = '_yoast_wpseo_metakeywords'
WHERE meta_key = '_genesis_keywords';

Here’s how it looks on phpMyAdmin panel:
MySQL's phpMyAdmin SQL commands

You have to make sure that there aren’t any typos whatsoever on the value you need to assign on the meta_key or else you need to restore the database from the backup and start over.

These sql statements were tested on WordPress SEO version 1.4.24 and Genesis Framework 2.0.2. If you use older or newer versions of both software, you need to verify the meta_key if they’re still using the same values.

January 29, 2014 Filed Under: WordPress Tagged With: Genesis Framework, SQL

How to Add Pagination on Genesis Lifestyle Theme Front Page

Genesis Lifestyle Pro Theme
As an update to their top seller Genesis Theme, StudioPress released a new Lifestyle Pro Theme with responsive design and HTML5. If you’re a member of My StudioPress, then you’ll have access to setup the theme exactly as it looks like in a demo, which is already very good to begin with.

I really like how the front page was setup, however I also want it to function as my main blog along with navigation links at the bottom of the content (see picture). Genesis standard loop is still going to be used for page two and beyond so they look like a normal blog page.

In order to achieve this, we need to modify the front-page.php file. The modification was based on the code from Bill Erickson for creating custom loop with pagination.

See the modified front-page.php below with all changes highlighted.

<?php
/**
 * This file adds the Home Page to the Lifestyle Pro Theme.
 *
 * @author StudioPress
 * @package Lifestyle Pro
 * @subpackage Customizations
 */

add_action( 'genesis_meta', 'lifestyle_home_genesis_meta' );
/**
 * Add widget support for homepage. If no widgets active, display the default loop.
 *
 */
function lifestyle_home_genesis_meta() {

	if ( !is_paged() ) {

		if ( is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom-left' ) || is_active_sidebar( 'home-bottom-right' ) ) {
	
			// Force content-sidebar layout setting
			add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_content_sidebar' );
	
			// Add lifestyle-pro-home body class
			add_filter( 'body_class', 'lifestyle_body_class' );
	
			// Remove the default Genesis loop
			remove_action( 'genesis_loop', 'genesis_do_loop' );
	
			// Add homepage widgets
			add_action( 'genesis_loop', 'lifestyle_homepage_widgets' );

		}
	}
}

function lifestyle_body_class( $classes ) {

	$classes[] = 'lifestyle-pro-home';
	return $classes;
	
}

function lifestyle_homepage_widgets() {
	
	genesis_widget_area( 'home-top', array(
		'before' => '<div class="home-top widget-area">',
		'after'  => '</div>',
	) );
	
	genesis_widget_area( 'home-middle', array(
		'before' => '<div class="home-middle widget-area">',
		'after'  => '</div>',
	) );
	
	if ( is_active_sidebar( 'home-bottom-left' ) || is_active_sidebar( 'home-bottom-right' ) ) {

		echo '<div class="home-bottom">';

		genesis_widget_area( 'home-bottom-left', array(
			'before' => '<div class="home-bottom-left widget-area">',
			'after'  => '</div>',
		) );

		genesis_widget_area( 'home-bottom-right', array(
			'before' => '<div class="home-bottom-right widget-area">',
			'after'  => '</div>',
		) );
		
		echo '</div>';	
	}

	// Based on https://gist.github.com/billerickson/3218052
	global $post;
	// arguments, adjust as needed
	$args = array(
				'post_type' => 'post',
				'posts_per_page' => get_option( 'posts_per_page' ),
				'post_status' => 'publish',
				'paged' => get_query_var( 'paged' )
			);

	/*
	Overwrite $wp_query with our new query.
	The only reason we're doing this is so the pagination functions work,
	since they use $wp_query. If pagination wasn't an issue,
	use: https://gist.github.com/3218106
	*/
	
	global $wp_query;
	$wp_query = new WP_Query( $args );
		 
	if ( have_posts() ) :
		while ( have_posts() ) : the_post();
		endwhile;
		
		do_action( 'genesis_after_endwhile' );			
	endif;

	wp_reset_query();
}

genesis();

The pagination on the front page should look like the picture below.

Genesis Lifestyle Pro Theme Custom Pagination

Lifestyle Pro is definitely worth checking out due to its capability as one of the modern responsive WordPress themes, its elegant look, and easy customization.

Caveat

You need to pay attention on the number posts to show on the Admin settings. Since the way it’s setup on the front page is showing 8 posts at a time, you need to make sure the setting is also for 8 posts at a time so the second page of the blog doesn’t skip any postings.

WordPress Admin Setup (Reading)

January 13, 2014 Filed Under: How To, WordPress Tagged With: Genesis Framework

Next Page »
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