WP-CLI: WordPress Command Line
WP-CLI is the official command line interface of WordPress. You can do many dashboard jobs with a short command: install plugins, create users, update the site, move the database and more. It saves a lot of time and is very good for scripts and for many sites. We used it in the chapters before, and here we see it in one place.
Install WP-CLI
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --version
Many hosts already have it. Run the commands from the folder of your WordPress site, or give the folder with --path=.
$ wp cli version
WP-CLI 2.12.0
$ wp –info | grep -E "PHP version|WP-CLI version"
PHP version: 8.3.36
WP-CLI version: 2.12.0
Get help
$ wp help | head -12
NAME
wp
DESCRIPTION
Manage WordPress through the command-line.
SYNOPSIS
wp <command>
$ wp help post create | head -8
NAME
wp post create
DESCRIPTION
Creates a new post.
Most used commands
| Command | What it does |
|---|---|
| wp core version | Show the WordPress version |
| wp core update | Update WordPress |
| wp plugin install name –activate | Install and activate a plugin |
| wp plugin list | List the plugins with status and version |
| wp theme activate name | Switch the theme |
| wp user create | Add a user |
| wp post create | Add a post or page |
| wp option get / update | Read or change an option |
| wp db export / import | Backup and restore the database |
| wp search-replace | Replace text in the database (for domain change) |
| wp cache flush | Clear the object cache |
| wp cron event list | See the scheduled tasks |
| wp eval ‘code’ | Run PHP code with WordPress loaded |
Plugins from the command line
$ wp plugin install classic-widgets
Installing Classic Widgets (0.3)
Downloading installation package from https://downloads.wordpress.org/plugin/classic-widgets.0.3.zip…
Using cached file '/tmp/wpcache/plugin/classic-widgets-0.3.zip'…
Unpacking the package…
Installing the plugin…
Plugin installed successfully.
Success: Installed 1 of 1 plugins.
$ wp plugin activate classic-widgets
Plugin 'classic-widgets' activated.
Success: Activated 1 of 1 plugins.
$ wp plugin list –fields=name,status,version
name status version
akismet inactive 5.7.2
classic-widgets active 0.3
hello inactive 1.7.2
$ wp plugin deactivate classic-widgets
Plugin 'classic-widgets' deactivated.
Success: Deactivated 1 of 1 plugins.
$ wp plugin delete classic-widgets
Deleted 'classic-widgets' plugin.
Success: Deleted 1 of 1 plugins.
Search and replace (move to a new domain)
After a move you must change the old domain to the new one in the database. Never do this with a plain SQL replace, because WordPress saves some data in a serialized form, and WP-CLI handles that safely. Always try --dry-run first:
$ wp post create –post_title="Link demo" –post_content='<a href="http://example.com/old-page/">Old page</a>' –post_status=publish –porcelain
5
$ wp search-replace 'http://example.com' 'https://new-example.com' –dry-run –skip-columns=guid –report-changed-only
Table Column Replacements Type
wp_options option_value 2 PHP
wp_posts post_content 3 SQL
wp_users user_url 1 SQL
Success: 6 replacements to be made.
Scheduled tasks and PHP code
$ wp cron event list –fields=hook,recurrence
hook recurrence
recovery_mode_clean_expired_keys 1 day
wp_privacy_delete_old_export_files 1 hour
wp_privacy_personal_data_cleanup_requests 1 day
wp_delete_temp_updater_backups 1 week
do_pings Non-repeating
wp_version_check 12 hours
wp_update_plugins 12 hours
wp_update_themes 12 hours
wp_site_health_scheduled_check 1 week
$ wp eval 'echo "Posts: " . wp_count_posts()->publish;'
Posts: 2
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |