Wordpress WP-CLI

Wordpress WP-CLI


Really important WP-CLI Notes

Run a WP-CLI command one or more sites on WordPress multisite - Daniel Bachhuber https://make.wordpress.org/cli/handbook/references/config/#global-parameters

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
1
2
sudo curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar --output /usr/local/bin/wp
sudo chmod +x /usr/local/bin/wp

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.

WP-CLI Packages

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:

You can discover more of those at Packagist

date 21. Dec 2016 | modified 25. Jan 2023
filename: Wordpress » Env » WP-CLI