Categories
Code Snippets General PHP Wordpress

[] operator not supported for strings – PHP

I had a problem with Revolution Slider on a WordPress installation when upgrading to PHP 7.2+. It would generate the error – PHP Fatal error: [] operator not supported for strings in C:[path]\wp-content\plugins\revslider\includes\framework\base-admin.class.php:71

The problem was from the plugin attempting to use the short array push syntax on a string:

$box = array();
$box['title'] = $title;
$box['location'] = $location;
$box['content'] = $content;
$box['draw_function'] = $customDrawFunction;
self::$arrMetaBoxes[] = $box;

The solution was to modify the last line as such:

self::$arrMetaBoxes = $box;

No explosions, so all is well!

Why was the operator not supported?

A discussion on Stack Overflow gave me a nudge in the right direction. The suggestion was that PHP 7 has problems with empty-index array push syntax.

These are ok and work properly in PHP 7+.

$undeclared_variable_name[] = 'value'; // creates an array and adds one entry

$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry

An attempt to use empty index push on any variable declared as a string, number, object, etc, doesn’t works however. ie

$declaredAsString = '';
$declaredAsString[] = 'value';

$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';

$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';

Theses examples all fail with a PHP error, which in my case was “[] operator not supported for strings”

For more PHP related posts, check here

Categories
Wordpress

WordPress: Cookies are blocked or not supported

WordPress Multisite sometimes gets into a login loop with a reauth querystring parameter due wo domain mapping: “Error: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress. ”

Solution 1 (Multisite):
Add this to your wp-config.php file:

define('COOKIE_DOMAIN', false);

Solution 2:
” I went into PhpMyAdmin > wp_usermeta > and deleted the meta_value for session_token. “

Solution 3:
It seems to be more frequent on multisite installations and on those it can take more steps. When the above fails, then adding the following lines to wp-config seems to work sometimes:

define(‘ADMIN_COOKIE_PATH’, ‘/’);
define(‘COOKIEPATH’, ”);
define(‘SITECOOKIEPATH’, ”);

There is a goodStackOverflow thread here: https://wordpress.stackexchange.com/questions/259839/cookies-in-multisite-where-network-sites-have-their-own-domain-name

Categories
Site Optimization Wordpress

Speed Optimization Tips From WPMU Dev

Pasting this here for future reference (from an email generated by WPMU Dev):

Site speed isn’t all about images though – there are heaps of other things to look at, too. I’ve collected a few posts from our blog that nicely sum up what you should look at to get your site operating at 100% optimal speed.

Here’s a quick overview of five areas to work on

This one is a comparison of a few top caching plugins (WP Super Cache, W3 Total Cache and WP Rocket)

A simple list of the 10 fastest WordPress themes

You like image optimization, so here’s some more detail about that

This is a big 22 step outline of every aspect of optimizing speed on your wordpress site

And finally, an in-depth drill-down on how we managed to get a WordPress site to a 91/100 on Google Pagespeed Insights

Categories
General PHP Wordpress

Basic WordPress plugin example with AJAX

Companion notes for: http://conceptclarity.ca/ajax-sample/

Content coming soon.