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
Code Snippets PHP Wordpress

WordPress Custom Sortable Columns in Post/Page/CPT Listings

Categories
Server Configuration Wordpress

405 method not allowed – IIS and Gutenberg

Some servers are not configured to allow all of the functionality for Gutenberg, the new WordPress editor. When saving, the PUT operation might be disallowed, giving you an error like: “405 method not allowed”. If you have access to your web.config file you can fix this problem with a minor change.

Change:

<add name="php-7.0.11" path="*.php" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:\Program Files\PHP Manager 1.2 for IIS 7\PHP-Versions\php-7.0.11-nts-Win32-VC14-x86\php-cgi.exe" resourceType="Either" requireAccess="Script" />

To:

<add name="php-7.0.11" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="C:\Program Files\PHP Manager 1.2 for IIS 7\PHP-Versions\php-7.0.11-nts-Win32-VC14-x86\php-cgi.exe" resourceType="Either" requireAccess="Script" />

Or, just add the “PUT”:

<add name="php-7.0.11" path="*.php" verb="GET,HEAD,POST,PUT,OPTIONS" modules="FastCgiModule" scriptProcessor="C:\Program Files\PHP Manager 1.2 for IIS 7\PHP-Versions\php-7.0.11-nts-Win32-VC14-x86\php-cgi.exe" resourceType="Either" requireAccess="Script" />

For more information about IIS and the “405 method not allowed error”, you can check out the official website at https://www.iis.net/

Edit: Updated January 27, 2019 to reflect that sometimes “OPTIONS” is required as well.

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
PHP Visual Studio Wordpress

WordPress IDE – Visual Studio Code

I have decided to test out another WordPress IDE – Visual Studio Code

*(or should I do PHP Tools for Visual Studio first?)

Step 1: Install the Visual Studio Code IDE from: https://code.visualstudio.com/

References:
Autocomplete and snippetrs: https://marketplace.visualstudio.com/items?itemName=tungvn.wordpress-snippet
Review with recommended plugins, etc: https://tommcfarlin.com/vs-code-wordpress/

https://code.visualstudio.com/docs/languages/php