Really important WP-CLI Notes
Detect whether WP-CLI is running - Daniel Bachhuber Always require a specific file when running WP-CLI - Daniel Bachhuber Update all sites on WordPress Multisite to https using WP-CLI - Daniel Bachhuber 10 advanced WP-CLI tricks - Daniel Bachhuber
Install WP-CLI globally
Test installation with:
wp --info or update by typing
wp cli update.
How to properly run WP CLI / Composer
It is very important to run those tools as the right user and not to have permission problems later on. Idea came from this article but I’m using in much simplified variation.
File and folder permissions are usually set to “no-login” user and that also presents an obstacle.
Best way to switch to proper user (www-data for example) is to execute
U=web2; runuser $U -s /bin/bash and there are also alternative ways like
sudo -u $U -s sh or
su $U -s /bin/bash
But the final solution, and the best one is to execute this:
runuser $(stat -c %U .) -s /bin/bash
This will first run
stat -c %U . to get the owner of the current folder, then it will pass this to
runuser to execute the command as this user.
Packages should be used if you don’t want to burden your site with a lot of plugins, for example debug plugins. WP-CLI package is a great solution as WP-CLI places installed packages in
~/.wp-cli/packages/, a hidden subdirectory for the user’s home directory, which means they have no relation to the WordPress installation.
Almost every important package can be found in Package Index, but any package with a URL can be installed even if it’s not in this index. Packages are installed with
wp package install <packagename> and then run as a normal WP-CLI command.
In case the Linux user for the site doesn’t have a proper home folder, such as in ISPConfig, you’ll have to install and run the package as another user, but not as root as it’s too risky.
For example, command
wp query-debug will count the database queries, and can help you determine which query may be causing performance issues.
Some interesting WP-CLI packages:
- wp-cli/doctor-command: Diagnose problems within WordPress by running a series of checks for symptoms
- wp-cli/profile-command: Quickly identify what’s slow with WordPress
- TypistTech/image-optimize-command: Easily optimize images using WP CLI
- wp-cli/media-command: Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
- wp-cli/language-command: Installs, activates, and manages language packs.
- wp-cli/extension-command: Manages plugins and themes, including installs, activations, and updates.
- wp-cli/maintenance-mode-command: Activates, deactivates or checks the status of the maintenance mode of a site.
You can discover more of those at Packagist