WP-CLI Cheat Sheet

WooCommerce

wp wc update – Updated WooCommerce database from command line.

LiteSpeed

wp lscache-purge all – Purge all cache.

Improve WordPress Comments Load time when using Akismet: Adding an index to wp_comments

A customer of mine was seeing a 15 second load time on their WordPress site when clicking on “Comments”.

I installed Query Monitor and saw that the Akismet plugin was running the following SQL command on each comment.

SELECT COUNT(*) FROM wp_comments WHERE user_id = 1820 AND comment_approved = 1;

This query was to show how many comments the user had previously had approved. The query was taking 0.5 seconds per comment listed when you visit the “Comments” page. The page was taking around 15 seconds to load at times. Now if you had this page set to load 100 comments per page, then you’re looking at 60 seconds.

I decided to run an explain on the query to figure out what was going on.

explain SELECT COUNT(*) FROM wp_comments WHERE user_id = 778 AND comment_approved = 1;

Which resulted in the following. (Sorry for the screenshot, copy/pasting and reformatting was not something I wanted to do. Also this references the wp_bspr_comments. This is due to the database having a wp_bspr for it’s table prefix.

MySQL Explain on wp_comments query not indexed

As you can see, the query is having to go through 19866 rows within the wp_comments table which is the total amount in the table. Since they’re using count(*) all rows are scanned.

So I decided to add an index for the user_id and comment_approved columns.

create index wp_comments_askismet ON wp_comments (user_id,comment_approved);

No when running an explain, there are only 9 rows scanned.

MySQL Explain on wp_comments with Aksimet after index

Load times were reduced dramatically. However, this can all be mitigated if you simply turn of the Akismet feature.

Akismet option to show number of approved comments beside each comment author.

WHMCS and Hexonet ISPAPI Registrar Module “Missing Required Attribute; X-CA-LEGALTYPE” Issue

If you’re running WHMCS and the Hexonet ISPAPI Registrar Module, you might have issues registering .ca domain names. An error will show up when you click on register, here’s a screenshot of the error.

hexonet-ispapi-dotca-error

Invalid attribute value; INVALID Contact [OWNERCONTACT] (Missing required attribute; X-CA-LEGALTYPE)]

This is due to the .ca registry requiring all registrations require a legal type designation, there’s multiple types. Commonly used types are Canadian Citizen and Corporation.

The fix is to open up the file “modules/registrars/ispapi/additionaldomainfields_sample.php” which is either in the downloadable zip file or already in your WHMCS installation. The file contains the following additions domain fields that you need to place under “WEBROOT/resources/domains/additionaldomainfields.php”

## .CA DOMAIN REQUIREMENTS ##
## add ispapi additional fields ##
$additionaldomainfields[“.ca”][] = array(
“Name” => “Legal Type”,
“LangVar” => “catldlegaltype”,
“Type” => “dropdown”,
“Options” => “Corporation,Canadian Citizen,Permanent Resident of Canada,Government,Canadian Educational Institution,Canadian Unincorporated Association,Canadian Hospital,Partnership Registered in Canada,Trade-mark registered in Canada,Canadian Trade Union,Canadian Political Party,Canadian Library Archive or Museum,Trust established in Canada,Aboriginal Peoples,Legal Representative of a Canadian Citizen,Official mark registered in Canada”,
“Default” => “Corporation”,
“Description” => “Legal type of registrant contact”,
“Ispapi-Name” => “X-CA-LEGALTYPE”,
“Ispapi-Options” => “CCO,CCT,RES,GOV,EDU,ASS,HOP,PRT,TDM,TRD,PLT,LAM,TRS,ABO,LGR,OMK”
);
$additionaldomainfields[“.ca”][] = array(
“Name” => “CIRA Agreement”,
“LangVar” => “catldciraagreement”,
“Type” => “tickbox”,
“Description” => “Tick to confirm you agree to the CIRA Registration Agreement shown below
You have read, understood and agree to the terms and conditions of the Registrant Agreement, and that CIRA may, from time to time and at its discretion, amend any or all of the terms and conditions of the Registrant Agreement, as CIRA deems appropriate, by posting a notice of the changes on the CIRA website and by sending a notice of any material changes to Registrant. You meet all the requirements of the Registrant Agreement to be a Registrant, to apply for the registration of a Domain Name Registration, and to hold and maintain a Domain Name Registration, including without limitation CIRA’s Canadian Presence Requirements for Registrants, at: www.cira.ca/assets/Documents/Legal/Registrants/CPR.pdf. CIRA will collect, use and disclose your personal information, as set out in CIRA’s Privacy Policy, at: www.cira.ca/assets/Documents/Legal/Registrants/privacy.pdf
“, “Required” => true, “Ispapi-Name” => “X-CA-ACCEPT-AGREEMENT-VERSION”, “Ispapi-Eval” => ‘if ( $value ) { $value = “2.0”; } else { $value = “”; }’ ); $additionaldomainfields[“.ca”][] = array( “Name” => “WHOIS Opt-out”, “LangVar” => “catldwhoisoptout”, “Type” => “tickbox”, “Description” => “Tick to hide your contact information in CIRA WHOIS (only available to individuals)”, “Ispapi-Name” => “OWNERCONTACT0X-CA-DISCLOSE”, “Ispapi-Eval” => ‘if ( $value ) { $value = “0”; } else { $value = “1”; }’ ); $additionaldomainfields[“.ca”][] = array( “Name” => “Trademark Number”, “Type” => “text”, “Size” => “50”, “Default” => “”, “Required” => false, “Ispapi-Name” => “X-CA-DOMAIN-TRADEMARK”, “Ispapi-Eval” => ‘if ( $value ) { $value = “Y”; } else { $value = “N”; }’ ); $additionaldomainfields[“.ca”][] = array( “Name” => “Contact Language”, “Type” => “dropdown”, “Options” => “English,French”, “Default” => “English”, “Ispapi-Name” => “X-CA-LANGUAGE”, “Ispapi-Options” => “EN,FR” );

WordPress Speed Up Analysis 2019-06-03

The following was a post I made on the WordPress Speed Up FaceBook Group. I thought it was worth posting here in-case someone else might be looking for something similar.

Olly Bee Excellent! First load for me was a white page without CSS being applied. Which is weird, I haven’t gone to the site before. So you might want to watch out for that, or maybe you were just making changes.

You’re not loading many assets which is great, mostly due to breeze combining css and js (assumption here since I see breeze.css). Just to note minification isn’t required for a great experience for customers. It’ll get you get you good scores on GTMetrix/PageSpeed but you’ll have to way usability for great gtmetrix scores.

Ue the built in Dev Tools in Chrome and look specifically at the Network page and waterfalls. Find items that are taking a long time to load, and see if they have any cache headers. Reload the page and see if they’re loading faster and if the cache headers appear.

When looking at all the items loading, the document items are usually html or PHP. This is where server resources come into play. If you’re getting higher load times on documents, it’s most likely due to caching not being in-play. Or it’s just a dynamic page (PHP) that can’t be cached (WooCommerce checkout, but even then LiteSpeed can use ESI blocks which makes it easier). If you can’t cache a page, then the server resources come into play. Specifically CPU, PHP its a CPU hog. Facebook tried to fix this by implementing compiled PHP using HHVM, but ditched PHP all together. Each time you have to load a PHP page, it has to be compiled by the server. You want to limit this as much as possible. WordPress deals with this fine as a vanilla installation. Once you add plugins, load times start to increase.

When I first loaded your portfolio page, I noticed it took about 3ms to load all assets. I looked at all the headers of each request and found that the main request for /portfolio had a x-cache miss and a high load time. Once I refreshed the page this load time went down. So it’s possible caching was put into play here either through Varnish or NGiNX fastcgi cache. Hard to tell, would have to look at your stack more.

If there’s caching in play, a header should appear. Most of the time this is configured, but not always.

Once your cache is filled, it looks like your pages load faster. So you might need to tweak your cache TTL. Which just tells the cache how long to keep things. This is great for static pages, but for anything dynamic it might cause issues. Most caching plugins deal with this automatically. Even CloudFlare does well with this if you setup the proper page rules. CloudFlare by default doesn’t do well with a WordPress site.

Aside from that, I see that .tff files aren’t being cached and have 400ms load tims where all your images are loading within 20ms which is great. That’s because most of them are from your StackPath CDN. And they’re small, which helps!

Another issue I noted is that your query stringed static assets aren’t being cached. Which is a common issue, so you can either enable this in CloudFlare via a rule. Or you can look at a plugin like Clearfy or Perf Matters. If you want to save money, there is code out there to simply just do this specific task.

Also, with the above plugins you can remove the wp-emoji-release.min.js and dashicons.min.css from loading. Again you most likely aren’t using them, but test anyways.

Hope that helps, Cheers!

MySQL 8 and Persona 8 Tuning for WordPress

If you’re running MySQL 8 or Percona 8, then you’ll probably see lots of guides online that reference some out of data configuration that really only deals with functions in MySQL 5 that have been deprecated in MySQL 8 and Percona 8

Query Cache

No longer used, and removed from MySQL 8 and Percona 8, alternative is ProxySQL as per the following Percona article https://www.percona.com/blog/2018/02/07/proxysql-query-cache/

Creating Users and Databases in MySQL 8 and Percona 8

If you’ve started using MySQL 8, you’ll notice your usually commands for creating a user no longer function. That’s because it’s changed in MySQL 8, you need to now create a user, then grant privileges.

Let’s create a user

CREATE USER ‘newuser’@'localhost' IDENTIFIED WITH mysql_native_password BY ‘user_password';

The critical part here is the “mysql_native_password” if you don’t have this portion, you’ll get errors like the following.

PHP Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in mysql_test.php on line 8
PHP Warning: mysqli_connect(): (HY000/2054): The server requested authentication method unknown to the client in mysql_test.php on line 8

This is due to the default authentication plugin being set to sha2, you can change it back to native by adding the following into your my.cnf

default-authentication-plugin=mysql_native_password

Now let’s grant the user privileges.

GRANT ALL ON database_name.* TO ‘newuser’@'localhost';

Now confirm the privileges.

SHOW GRANTS FOR 'newuser’@'localhost';

Maybe you want to Update a Password

ALTER USER ‘root'@'localhost' IDENTIFIED BY 'MyNewPass';

Fix the “The package could not be installed. PCLZIP_ERR_BAD_FORMAT” WordPress Error

This is pretty straightforward, you’ll see this error when updating or installing a plugin.

[enlighter linenumbers=”true”]The package could not be installed. PCLZIP_ERR_BAD_FORMAT[/enlighter]

Basically the file WordPress is trying to download is either corrupt or you don’t have the PHP curl module installed. So simply install it, for Ubuntu this is:

[enlighter linenumbers=”true”]apt-get install php-curl[/enlighter]

or

[enlighter linenumbers=”true”]apt-get install php7.3-curl[/enlighter]

Searching WordPress MySQL Dump Files

From time to time, you’ll need to look at data within MySQL dump files, specifically I do it mostly with WordPress. There isn’t an easy way to search the dump unless you use grep. Here are some useful grep commands.

Pull out a table.

[enlighter linenumbers=”true”] grep ‘INSERT INTO `wp_bspr_users`’ dump.sql > /tmp/users.sql [/enlighter]

Format a table in a readable format.

[enlighter linenumbers=”true”]cat dump.sql | grep ‘INSERT INTO `wp_users`’ | sed ‘s/),/’$’n/g’ [/enlighter]

Now if you run a popular shell like csh, you can add the following function.

[enlighter linenumbers=”true”]msd () { grep “INSERT INTO \`$2\`” $1 | sed “s/),/\’$’\n/g” }[/enlighter]

[enlighter linenumbers=”true”]Then run the following[/enlighter]

msd dump.sql wp_users

This might work, don’t know!