If you’re a WordPress user, chances are you’ve heard of WP-CLI. For those who don’t know, WP-CLI is a set of command-line tools for managing WordPress sites. It can be used for everything from installing and updating plugins to creating and managing users. In this post, we’re going to explore some tips and tricks for using WP-CLI to manage your WordPress site. From basic commands to more advanced usage, we hope you find this post helpful in streamlining your workflow.
Why Use WP-CLI?
Because we can automate by using bash scripts and cron. Also, CLI and TUI are often faster than loading via web UI.
WP-CLI provides a set of commands that are equivalent to the WordPress dashboard. For example, the “plugin” command allows you to manage plugins from the command line. The “theme” command allows you to manage themes from the command line.
---
In addition to the core set of commands, WP-CLI also provides a set of community-developed commands. These commands are not part of the core WP-CLI project but are developed by members of the community and made available for use via the wp CLI package manager (CPM).
How to Install WP-CLI
Before you begin, you will need to have SSH access to your hosting account. In other words, you need to have your WordPress website running at least on a cheap VPS such as VPSDime. Of course, you can run your site on DigitalOcean, Linode, AWS or a dedicated server. Make sure you are in a UNIX-like environment (OS X, Linux, FreeBSD, Cygwin) with PHP 5.6 or later, and WordPress 3.7 or later.
1 2 3 4 5 6 7 | curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar php wp-cli.phar --info chmod +x wp-cli.phar ## cd /usr/local/bin/wp ## mkdir /usr/local/bin/wp sudo mv wp-cli.phar /usr/local/bin/wp wp --info |
It is easy to install WP-CLI.
Tips and Tricks for Using WP-CLI
If you’re a WordPress developer, then you know that the command line can be a powerful tool. And if you’re looking for a way to speed up your workflow, then WP-CLI is worth investing the time.
The wp help
and wp help command-name
(where command-name is a real command name, such as comment, or post) will give you an excerpt of what you can do. There are a few utilities in the official WP-CLI Github repository:
1 | https://github.com/wp-cli/wp-cli/tree/master/utils |
One of the useful utilities is tab completion. wget the file and add the line on your profile file:
1 2 | # wget https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash source /PATH/TO/wp-completion.bash |
You can do scaffold theme tests with WP-CLI :
1 2 3 | wp scaffold theme-tests your-theme-name cd your-theme-name phpunit |
tests/
will contains PHPUnit-specific files. bin/install-wp-tests.sh
is a setup script. .travis.yml
is useful on GitHub to run Travis tests. Original doc:
1 | https://developer.wordpress.org/cli/commands/scaffold/theme-tests/ |
By typing wp shell
, you can start an interactive session to evaluate PHP statements and expressions. Here is the official doc :
1 2 | https://developer.wordpress.org/cli/commands/shell/ https://github.com/wp-cli/shell-command |
I have forked a repo on some of the commonly used commands which may help you more:
1 | https://github.com/AbhishekGhosh/wp-cli-tips |
With WP-CLI and cron, you can use bash scripts to periodically delete the transients.