[] 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!

The explanation

This discussion tipped me in the right direction: https://stackoverflow.com/questions/5879675/problem-with-fatal-error-operator-not-supported-for-strings-in

Display Tiff files on the web

Do you want to display a TIFF image on a webpage? You can learn how here. TIFF files are not made for the web and are not well suited for the web. They used to work in many browsers but as of this writing, support is waning: http://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support. TIFF files are very large and contain layer information and are meant to preserve quality so they do not compress well. If you display TIFF files you will hurt your page performance.

However, sometimes situations arise where a client requires that their site display TIFF files, so you have to. But how?

How to Display TIFF Files

Enter UTIF.js which was created for the Photopea online photo editor, therefore this library processes your TIFF files and allows you to display them in HTML.

Display TIFF Files on a Website
This is a TIFF file

You can use TIFF images directly inside the <img> element and then, you just have to to call UTIF.replaceIMG() once at some point. Eg:

<body onload=”UTIF.replaceIMG()”>

<img src=”image.tif” /> <img src=”dog.tif” /> …

Display TIFF Files on a Web Site in Chrome and Firefox

If you need to display TIFF files on the web, you might have problems. TIFF files used to be displayed by all browsers, but as they are not really suitable for web use, support has been dropping. See Image Support by Browser:  https://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support

This makes sense of course, if you are only trying to use the TIFF files as images as part of your layout or content. TIFF files are often much larger than what you want if you are trying to have a decent page speed score and good user experience. This is why TIFF support on the web is being dropped and Chrome and Firefox, of the major browsers no longer support the format. In fact only Safari and Microsoft Edge do.

Display TIFF Files on the Web

But what if you have a legitimate need for displaying this format? I recently created a file management application where the requirement was to be able to quickly upload an image and have it displayed in their gallery for download. Many of the images were for print materials and  therefore in the TIFF format, so they wouldn’t display on Chrome or Firefox. Instead they would open a download window upon loading the page.

We could have had a field for the main image and then a thumbnail, but the requirement was a streamlined process where they didn’t even want to title or caption the images – they just named the files intuitively for the listings. Generating a converted thumbnail upon upload wasn’t an option as the hosting environment was restrictive and didn’t have the ImageMagick extension for PHP enabled. They were unwilling to change hosts.

After a lot of digging, I ended up finding UTIF.js which was created for the Photopea online photo editor. This library processes your TIFF files and allows you to display them in HTML. This was very easy to install and use. I had to modify the functionality a little bit as there was a delay in having all of the images loaded. Also some new images would be loaded via AJAX as the user navigates the folder tree That made the body onload implementation was unsuitable. Beyond that it was quick and easy to get going.

Other Options to Display TIFF files in Chrome and Firefox

 

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" />

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

WordPress Login Reauth Loop

Sometimes I run into this login loop and here is something that has worked:

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

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 best:

define(‘ADMIN_COOKIE_PATH’, ‘/’);
define(‘COOKIE_DOMAIN’, ”);
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

Thai Green Curry Paste

  • 1 stalk lemongrass, minced OR 3 Tbsp. prepared frozen or bottled lemongrass (available at Asian stores)
  • 1-3 green chilies, sliced (Thai green chilies OR jalapeno)
  • 1 shallot, sliced, OR 4 Tbsp. minced purple onion
  • 4-5 cloves garlic
  • 1 thumb-size piece of galangal OR ginger, thinly sliced
  • 1/2 cup chopped fresh coriander/cilantro leaves & stems
  • 1/2 cup fresh basil
  • 1/2 tsp. ground cumin
  • 1/2 tsp. ground white pepper (available in most supermarket spice aisles)
  • 1/2 tsp. ground coriander
  • 3 Tbsp. fish sauce; Vegetarians: substitute 1 Tbsp. soy sauce
  • 1 tsp. shrimp paste (available at Asian stores); Vegetarians: substitute 1/2 tsp. salt
  • 2 Tbsp. lime juice
  • 1 tsp. brown sugar
  • 3-4 Tbsp. coconut milk (enough to blend ingredients together)

 

 

 

  1. Place all ingredients in a food processor, chopper, or blender.
  2. Process well to form a fragrant Thai green curry paste. Taste-test it for salt and spice. If too salty, add a squeeze of fresh lime or lemon juice. Add more chili for more heat. Your curry paste is now ready to be used. You can bottle up any leftovers and keep it in the refrigerator for up to 1 week. Freeze thereafter. ENJOY!

Tips:

If using a pestle & mortar (as pictured): Pound all dry herbs & spices together to form a paste, then gradually add the wet ingredients, stirring until smooth.

Your curry paste is now ready to use (see below for cooking tips).
For Vegetarian/Vegan Paste: Instead of fish sauce and shrimp paste, add 1 Tbsp. soy sauce plus 1/3 tsp. salt, as stated in the ingredients list. Alternatively, you could add 3 Tbsp. soy sauce, but this tends to turn the paste brownish-green instead of bright green, as one of the reviewers of this recipe so helpfully noted.

For recipes using this curry paste, see my:, see: Thai Green Curry Chicken Recipe, OR Vegetarian Thai Green Curry (Vegan), OR follow the steps below.

To Make a Thai Green Curry with this Paste (+ More Cooking Tips):

  • Heat a wok or large frying pan or pot over medium-high heat. Add 2-3 Tbsp. oil and swirl around, then add the green curry paste. Stir-fry until fragrant (1 minute), then add 1 cup stock plus your curry ingredients (meat, seafood, or tofu/wheat gluten + vegetables). Simmer until ingredients are cooked, gradually adding 1/2 to 1 can coconut milk until you’re happy with the taste/thickness/amount of sauce.
  • As you’re cooking, you can also add 2-3 kaffir lime leaves (left whole) as well as any leftover lemongrass stalk pieces for even more flavor.
  • When your curry is done, always perform a taste-taste for salt and spice. Add more fish or soy sauce if not salty enough, or some fresh-cut Thai red chilies if not spicy enough (OR add some dried crushed chili). If too spicy, add more coconut milk. If too salty or sweet, add a squeeze of lime juice. If too sour for your taste, add a touch more sugar. Finish your curry by sprinkling over generous amounts of fresh basil.

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

Bulk Migrate Users to New Blog on WordPress Multisite

Recently I had to move a large number of users from one blog to another on a Multisite install. These users had been mistakenly added to the wrong blog and most of them also had been assigned the wrong role. There were plugin options to do this one by one, but not to bulk migrate users. There were too many users to do it manually.
I needed to get a list of users in the source blog, filter out the users that I didn’t want to modify, move the ones I did, then delete the users that had been moved from the source blog.

Code to Bulk Migrate Users:

$targetblog = 3; // The id of my target blog
 $role = 'agent'; // Custom role
 
 // First I retrieved all of the users in the source blog
 $blogusers = get_users( 'blog_id=1' ); 
 
 // Loop through the Array of WP_User objects.
 foreach ( $blogusers as $user ) {

 // I wanted to only migrate users that weren't already members of the target blog
 // In this case there were only subscribers, agents, and administrators in the database. 
 // Each user had exactly one role. I wanted to move subscribers and agents, 
 // so I used the following check:
 if (! is_user_member_of_blog( $user->ID, $targetblog ) && $user->roles[0] != 'administrator'){
 
 // The following line is only there to give me a bit of a log of events, and to verify that 
 // the correct users were been moved, prior to doing the actual migration operation
 echo '<hr><span>Migrating: ' . $user->ID. ' - ' . esc_html( $user->user_email ) . ' - ' .$user->roles[0] .'</span><br>';
 
 if ( add_user_to_blog( $targetblog, $user->ID, $role ) ) {
 echo 'Added user '.$user->ID.' as '.$role;
 // remove the user from the source blog, and asign their posts to user ID 2
 if ( remove_user_from_blog($user->ID, 1, 2) ) {
 echo 'Removed from source blog';
 } else {
 echo 'Failed to remove user ';
 }
 } else {
 echo 'Failed to add user '.$user->ID.' as '.$role;
 }
 }
 
 }

Some of the important WordPress functions needed to bulk migrate users are:
is_user_member_of_blog
add_user_to_blog
remove_user_from_blog

…among others!

Enabling SSL Over TLS For Filezilla Server

In order to encrypt communications via FTP, which is obviously more secure,  you should connect to a server using FTP over TLS. When you are using Filezilla Server this process is quite easy and can be completed in under 5 minutes.

The certificate in the following steps will generate warnings as it isn’t issued by a trusted authority, but for the sake of simply connecting to an endpoint that you already trust and making sure your transfers are secure, this is the way to go:

See: https://wiki.filezilla-project.org/FTP_over_TLS