Essential WPCli Commands
How to install
Use curl to download the WP-CLI phar file:
1
2
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Check if the file is executable:
1
2
php wp-cli.phar --info
Move the downloaded file to a directory in your system’s PATH (e.g., /usr/local/bin/):
1
sudo mv wp-cli.phar /usr/local/bin/wp
Make it executable:
1
sudo chmod +x /usr/local/bin/wp
test install
1
wp --info
Create child theme
1
wp scaffold child-theme sample-theme --parent_theme=twentysixteen
Content Commands
1
wp post create --post_type=page --post_title="Your Page Title" --post_content="Your page content goes here." --post_status=publish
Database commands
Delete database
This command drops the database. No name is required as wpcli looks in the wp-config file to find the database name. You can run this command from anywhere within your Wordpress file structure.
1
wp db drop
Export database
Will export an SQL file to your Wordpress root directory.
1
wp db export
Export database into custom named file
The default will be to use the database name
1
wp db export custom_filename.sql
Import database
Ensure to provide the correct path to the file.
1
wp db import wordpress_dbase.sql
Search and Replace database
Find all occurences of a string in a database and replace it with another value. This can be really useful when you are migrating a site to a different domain.
1
wp search-replace 'http://example.test' 'http://example.com'
Plugin commands
List plugins
1
wp plugin list
Delete plugin
1
wp plugin delete akismet
Install Plugin
Here we install the query monitor plugin
1
wp plugin install query-monitor
Update All Plugins
1
wp plugin update --all
Update WP Core
1
wp core update
Flush the Permalinks
1
wp rewrite flush
User commands
Create a wp user account
1
wp user create bob bob@example.com --role=author
Get the ID of the user
This command is important for running other user related functions that take the user id as a requirement.
1
wp user list
Update the password of the user
Once you have the user ID then you can update the users password
1
wp user update 7 --user_pass=new_pass
Generate posts
Generate dummy / demo content
1
wp post generate --count=10 --post_type=page --post_date=1999-01-04
Regenerate Image sizes
As you may already know Wordpress supports custom image sizes. Now when you add a new custom image size your site, you will need to regenerate the images so that your new custom image files in the new size exists.
1
wp media regenerate --image_size=large
Older Gist
Create User - Password Generated
1
wp user create your_username ex@example.com.au --role=administrator
Gravity Forms Create Field - Requires Addon
1
wp gf field create 1 type label
Create Form in Gravity Forms
1
wp gf form create 'My Form Title' 'Description Here'
WP Admin
Open /wp-admin/
in a browser.
WP Cache
Adds, removes, fetches, and flushes the WP Object Cache object.
Available Commands
wp cap
: Adds, removes, and lists capabilities of a user role.wp cli
: Review current WP-CLI info, check for updates, or see defined aliases.wp comment
: Creates, updates, deletes, and moderates comments.wp config
: Generates and reads thewp-config.php
file.wp core
: Downloads, installs, updates, and manages a WordPress installation.wp cron
: Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules.wp db
: Performs basic database operations using credentials stored inwp-config.php
.wp search-replace
:<old>
: A string to search for within the database.<new>
: Replace instances of the first string with this new string.[<table>…]
: List of database tables to restrict the replacement to. Wildcards are supported, e.g.wp\__options
orwp_post_
.[--dry-run]
: Run the entire search/replace operation and show report, but don’t save changes to the database.
wp dist-archive
: Create a distribution archive based on a project’s.distignore
file.wp embed
: Inspects oEmbed providers, clears embed cache, and more.wp eval
: Executes arbitrary PHP code.wp eval-file
: Loads and executes a PHP file.wp export
: Exports WordPress content to a WXR file.wp find
: Find WordPress installations on the filesystem.wp help
: Get help on WP-CLI, or on a specific command.wp import
: Imports content from a given WXR file.wp language
: Installs, activates, and manages language packs.wp media
: Imports files as attachments, regenerates thumbnails, or lists registered image sizes.wp menu
: Lists, creates, assigns, and deletes the active theme’s navigation menus.wp network
: Perform network-wide operations.wp option
: Retrieves and sets site options, including plugin and WordPress settings.wp package
: Lists, installs, and removes WP-CLI packages.wp plugin
:- Manages plugins, including installs, activations, and updates.
- Examples:
1 2
wp plugin delete akismet wp plugin install query-monitor
wp post
: Manages posts, content, and meta.wp post-type
: Retrieves details on the site’s registered post types.wp profile
:wp rewrite
: Lists or flushes the site’s rewrite rules, updates the permalink structure.wp role
: Manages user roles, including creating new roles and resetting to defaults.wp scaffold
: Generates code for post types, taxonomies, plugins, child themes, etc.wp search-replace
: Searches/replaces strings in the database.wp server
: Launches PHP’s built-in web server for a specific WordPress installation.wp shell
: Opens an interactive PHP console for running and testing PHP code.wp sidebar
: Lists registered sidebars.wp site
:- Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
wp super-admin
:- Lists, adds, or removes super admin users on a multisite installation.
wp taxonomy
: Retrieves information about registered taxonomies.wp term
: Manages taxonomy terms and term meta, with create, delete, and list commands.wp theme
:- Manages themes, including installs, activations, and updates.
wp transient
:- Adds, gets, and deletes entries in the WordPress Transient Cache.
wp user
:- Manages users, along with their roles, capabilities, and meta.
- Example:
1
wp user create instructor email@yoursite.com.au --role=instructor
wp widget
: Manages widgets, including adding and moving them within sidebars.
setup wordpress
wp core download
Troubleshooting
If you are getting some errors when trying to update plugins it could be due to permissions. Some plugins also require a valid license to update.
To fix a permission related issue. See the link below to reset the Wordpress file and directory permissions.
see wp file permissions commands
Switch to non root user
If you have installed WpCLI on a webserver.
Before running any wpcli commands you probably want to switch to a non-root so that you are not flagged with warnings when running these commands as root
.
Switch to non-root user
1
su non-root
Install WordPress with WpCLI
WpCLI Plays an important role when you installing WordPress on a barebones VPS. As there is no control panel there is no user interface for creating and installing the database. WpCLI steps into to help out and makes installation of Wordpress on a barebones VPS efficient and easy.
Install WordPress website on a VPS using WPCli
Official Documentation
WPCLI installed
Update wordpress with cron job
WP-CLI (WordPress Command Line Interface) is a powerful tool for managing WordPress installations from the command line. To set up a cron job on your Ubuntu server to keep your WordPress websites up to date using WP-CLI, follow these steps:
1. Install WP-CLI
If you haven’t already, install WP-CLI on your Ubuntu server by running the following command:
1
sudo apt-get update && sudo apt-get install -y wp-cli
This will install WP-CLI and its dependencies.
2. Create a script to update WordPress
Create a new file (e.g., update_wordpress.sh
) with the following contents:
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
# Define the path to your WordPress installation(s)
WP_PATHS="/var/www/website1 /var/www/website2 /var/www/website3"
# Loop through each WordPress installation and update it
for WP_PATH in $WP_PATHS; do
cd $WP_PATH
wp core update
wp plugin update --all
wp theme update --all
done
Replace /var/www/website1
, /var/www/website2
, etc. with the actual paths to your
WordPress installations.
Make the script executable by running:
1
chmod +x update_wordpress.sh
3. Add a cron job
Open the crontab editor for the root user (since WP-CLI requires root privileges):
1
sudo crontab -e
Add the following line to schedule the script to run daily at 2:00 AM:
0 2 * * * /path/to/update_wordpress.sh
Replace /path/to/
with the actual path where you saved the update_wordpress.sh
script.
Save and exit the editor. The cron job will now run automatically every day at 2:00 AM to update your WordPress installations using WP-CLI.
Tips and Variations
- If you want to update only specific plugins or themes, modify the
wp plugin update
andwp theme update
commands accordingly. - To receive email notifications when updates are applied,
- add the following line before the
done
statement in the script:echo "WordPress updates applied" | mail -s "WordPress Updates" your_email@example.com
- If you have multiple WordPress installations with different versions (e.g., some using PHP
7.2 and others using PHP 7.4), make sure to update the
WP_PATHS
variable accordingly, and consider creating separate scripts or cron jobs for each version. - Consider adding a log file to track updates and errors by modifying the script to append output to a file (e.g.,
>> /var/log/wordpress_updates.log
)
Run the bash script manually
1
bash /var/www/update_wordpress.sh