WP-CLI commands cheat sheet
Getting started and info
-
wp --infoShow information about the environment WP-CLI runs in, including the PHP version, the operating system, and the paths of the tool's configuration files.
-
wp cli versionShow the installed version of WP-CLI.
-
wp helpList the available commands with a short description of each one.
-
wp help plugin installShow the full documentation of a specific command, including all its options and usage examples.
-
wp core is-installed && echo "installed"Check whether WordPress is installed in the current directory. The command itself prints nothing and only returns an exit code, 0 when WordPress is installed and 1 when it is not, so the example adds echo to print a result.
-
wp plugin list --path=/var/www/example.com/public_htmlList the plugins of an installation in another directory, without changing to that directory. The --path option works with every command.
-
wp option get home --url=https://example.comShow the home URL of a specific site. The --url option sets which site the command runs on, and you need it mainly in a network of sites (Multisite).
-
sudo -u www-data wp plugin listRun the command as the user who owns the site files, so new files are created with the correct ownership. In this example the user is www-data, and the username varies from server to server.
-
wp plugin list --allow-rootAllow the command to run as the root user, which WP-CLI blocks by default. Caution: when you run it this way, the code of every plugin and theme runs with full privileges, and new files are owned by root.
WordPress core
-
wp core versionShow the installed version of WordPress.
-
wp core check-updateCheck whether a new version of WordPress is available and list the available updates, without installing them.
-
wp core updateUpdate WordPress to the latest version. Before you update, back up the files and the database.
-
wp core update --minorInstall only maintenance and security updates within the current version, without moving to a new major version.
-
wp core update-dbRun the WordPress database update, which adapts the database structure to the installed version. You need this command when the WordPress admin area asks you to update the database, for example after an update done by copying files.
-
wp core download --locale=en_USDownload the WordPress files in the language set with --locale, here US English, to the current directory. The command does not overwrite an existing installation unless you add the --force option.
-
wp config create --dbname=wp_db --dbuser=wp_user --prompt=dbpassCreate a wp-config.php file with the database connection details. The --prompt option asks you to type the password, so it is not saved in the command history.
-
wp core install --url=https://example.com --title="My Site" --admin_user=admin --admin_email=user@example.com --prompt=admin_passwordComplete the WordPress installation and create the admin user, without going through the installation wizard in the browser. Before you run it, you need a wp-config.php file and an existing database, and the --skip-email option prevents the email notification to the admin.
-
wp core verify-checksumsCompare the core files with the official checksums and list files that were modified, are missing, or were added. The command helps you find malicious code injected into the core files.
-
wp language core install fr_FR --activateInstall a core language pack, in this example French, and set it as the site language. When the language pack is already installed, the wp site switch-language fr_FR command sets it as the site language.
Plugins
-
wp plugin listList all installed plugins, including the status, the version, and whether an update is available.
-
wp plugin list --update=availableList only the plugins that have an update available.
-
wp plugin list --status=active --field=nameList only the names of the active plugins, one line per plugin. This output is convenient for scripts.
-
wp plugin install plugin-name --activateInstall a plugin from the official directory by its slug and activate it right away.
-
wp plugin install ./plugin-name.zip --activateInstall a plugin from a local ZIP file and activate it. Instead of a local path, you can also specify the URL of a ZIP file.
-
wp plugin activate plugin-nameActivate an installed plugin.
-
wp plugin deactivate plugin-nameDeactivate a plugin without deleting its files or settings.
-
wp plugin status plugin-nameShow the plugin details, including the status, the version, the author, and the description.
-
wp plugin update --all --dry-runShow which plugins would be updated and to which version, without actually updating them.
-
wp plugin update --allUpdate all plugins that have an update available. Back up the site before you update, and check the main pages afterward.
-
wp plugin verify-checksums --allCompare the plugin files with the checksums in the official directory and list files that were modified. The check is available only for plugins from the official directory, and for other plugins a message says that the checksums are not available.
-
wp plugin delete plugin-nameDelete the plugin files without running its uninstall routine, so the settings and tables the plugin created stay in the database. Caution: you cannot undo the deletion.
-
wp plugin uninstall plugin-name --deactivateDeactivate the plugin, run the uninstall routine defined in it, and delete its files. Caution: the uninstall routine may also delete the plugin's data and settings.
Themes
-
wp theme listList all installed themes, including the active theme and the available updates.
-
wp theme install theme-nameInstall a theme from the official directory by its slug, without activating it.
-
wp theme install ./theme-name.zip --activateInstall a theme from a local ZIP file and activate it right away.
-
wp theme activate theme-nameSwitch the active theme of the site.
-
wp theme update --allUpdate all themes that have an update available. The update overwrites changes made directly in the theme files, so keep those changes in a child theme.
-
wp theme delete theme-nameDelete a theme that is not active. Caution: you cannot undo the deletion, and an active theme is deleted only with the --force option.
Users
-
wp user listList all the users of the site, including the ID, the username, the email address, and the role.
-
wp user list --role=administratorList only the users with the administrator role. Checking this list regularly helps you spot admin accounts you do not recognize.
-
wp user create username user@example.com --role=editorCreate a new user with the editor role. When no password is set, WP-CLI generates a random password and shows it at the end.
-
wp user update admin --prompt=user_passChange a user's password with a prompt to type it, so it is not saved in the command history. By default, WordPress emails the user about the password change, and the --skip-email option turns off this email.
-
wp user reset-password admin --skip-email --show-passwordGenerate a new random password for the user and show it in the output. Without the --skip-email option, the user gets an email about the password change, and without the --show-password option, the new password is not shown.
-
wp user set-role username authorReplace a user's role with another role and remove the user's previous roles.
-
wp user add-role username editorAdd another role to a user, without removing the user's existing roles.
-
wp user session destroy admin --allLog the user out of every device they are logged in on. The command is useful after you change a password because of a suspected break-in.
-
wp user delete username --reassign=1Delete a user and reassign their content to the user with ID 1. Caution: without the --reassign option, all the content the user created is also deleted.
Database
-
wp db export backup.sqlExport the site database to an SQL file in the current directory. When you run the command from the site's public directory, move the backup file out of that directory.
-
wp db export - | gzip > backup.sql.gzExport the database and compress it straight into a gz file, without creating an uncompressed intermediate file.
-
wp db import backup.sqlImport an SQL file into the site database. Caution: existing tables with the same name are overwritten, so export a fresh backup before you import.
-
wp db checkCheck the tables in the database and report damaged tables.
-
wp db repairTry to repair damaged tables. The InnoDB storage engine does not support this repair, and for tables of that type the command only reports it.
-
wp db optimizeOptimize the tables and free the space left over after data was deleted. On large tables, this may take a long time.
-
wp db size --human-readableShow the total size of the database in readable units, such as MB.
-
wp db size --tables --human-readableShow the size of each table separately, which helps you spot tables that have grown unusually large.
-
wp db query "SELECT ID, post_title FROM wp_posts WHERE post_status = 'publish' LIMIT 10"Run an SQL query directly on the database. Replace the wp_ prefix with the prefix set for the site, which the wp db prefix command shows.
-
wp db search "example.com"Search for text in all the WordPress tables and show the table, column, and row of each match.
-
wp search-replace "http://example.com" "https://example.com" --dry-runShow how many replacements would be made in each table and column, without changing anything. Run this check before every replacement.
-
wp search-replace "http://example.com" "https://example.com"Replace text in all the WordPress tables, including data stored in serialized format, which a plain SQL replacement breaks. Caution: before you replace, export a database backup and run the same command with --dry-run.
-
wp search-replace "http://example.com" "https://example.com" --skip-columns=guidRun the replacement and skip the guid column, which serves as a permanent ID for posts and for items in the RSS feed. Caution: test the replacement first with --dry-run.
-
wp search-replace "old.example.com" "example.com" --all-tablesRun the replacement on all the tables in the database, regardless of the prefix, including tables created by plugins. Caution: when several sites share the same database, the replacement also changes their tables, and the --all-tables-with-prefix option limits it to the tables with the site's prefix.
-
wp search-replace "old.example.com" "example.com" --preciseProcess all the columns in PHP rather than in an SQL query, which is slower but more thorough. The option is useful when some occurrences were not replaced in a regular run, and like the other replacements, test it first with --dry-run.
-
wp db reset --yesCaution: this deletes all the tables in the database and recreates it empty. The --yes option skips the confirmation prompt, and without a backup you cannot undo the action.
Options and settings
-
wp option get siteurlShow the address where the WordPress files are installed.
-
wp option get homeShow the site address that visitors see. On most sites, it is the same as the siteurl value.
-
wp option update siteurl "https://example.com"Change the address of the WordPress files. This change updates only the setting itself, and the wp search-replace command updates the links in the content.
-
wp option update home "https://example.com"Change the site address. Wrong values in siteurl or home are a common cause of redirect loops and of losing access to the WordPress admin area, and the WP_HOME or WP_SITEURL constants in wp-config.php override the values in the database.
-
wp option update blog_public 0Ask search engines not to show the site in search results, for example on a development site. The value 1 restores the normal setting.
-
wp config listList all the constants and variables defined in the wp-config.php file. Caution: the output includes the database password and the security keys.
-
wp config get WP_DEBUGShow the value of a specific constant in the wp-config.php file.
-
wp config set WP_DEBUG true --rawTurn on WordPress debug mode. The --raw option saves the value as true and not as a text string.
-
wp config set WP_DEBUG_LOG true --rawMake WordPress save errors to the debug.log file in the wp-content directory while debug mode is on. The file may be accessible from the browser, so delete it when you finish testing.
-
wp config set DISALLOW_FILE_EDIT true --rawTurn off the plugin and theme file editor in the WordPress admin area, so nobody can edit code through the browser.
-
wp config shuffle-saltsReplace the security keys (salts) in the wp-config.php file with new random values. All users are logged out and must log in again.
Content
-
wp post list --post_type=page --fields=ID,post_title,post_statusList the pages of the site with the ID, title, and status of each page.
-
wp post delete 123Move a post to the trash. You can restore it from the trash in the WordPress admin area.
-
wp post delete 123 --forceCaution: this deletes the post permanently, without moving it to the trash.
-
wp post delete $(wp post list --post_status=trash --format=ids) --forceCaution: this permanently deletes all the posts in the trash. The command works on posts only, and for pages, add --post_type=page to the inner command.
-
wp post delete $(wp post list --post_type=revision --format=ids) --forceDelete all the revisions of posts and pages. Caution: after the deletion, you cannot restore an earlier version of the content.
-
wp post generate --count=10Create ten sample posts with random content, for testing a design on a development site. The command is not meant for a live site.
-
wp comment delete $(wp comment list --status=spam --format=ids) --forceCaution: this permanently deletes all the comments marked as spam, without moving them to the trash.
-
wp media regenerate --yesRegenerate all the image sizes, for example after you switch to a theme that defines new sizes. Caution: by default, the command deletes old sizes that are no longer registered, and images in the content that link to them may stop showing, unless you add the --skip-delete option.
-
wp media regenerate --only-missing --yesCreate only the missing image sizes, without regenerating existing sizes, so it runs faster.
-
wp export --dir=./exportExport the content to an XML file in WXR format, which you can import into another WordPress site with the import tool. Create the export directory before you run the command.
Cache and cron
-
wp cache flushFlush the WordPress object cache. When a persistent object cache, such as Redis or Memcached, is configured, the command may flush the entire cache in that service, including data of other sites that use the same service.
-
wp transient delete --expiredDelete only the expired transients.
-
wp transient delete --allDelete all transients. WordPress and the plugins recreate them as needed, so the first load after the deletion may be slower.
-
wp rewrite flushRebuild the permalink rules. The command may fix 404 errors on existing pages, for example after a site migration.
-
wp rewrite structure "/%postname%/"Set the permalink structure so that the post URL contains only its slug.
-
wp cron event listList all the WordPress scheduled tasks and the next run time of each task.
-
wp cron event run --due-nowImmediately run all the tasks whose scheduled time has arrived.
-
wp cron testCheck whether WordPress cron can run on the site and report problems.
-
wp config set DISABLE_WP_CRON true --rawStop scheduled tasks from running on every page load. After this change, set up a cron job on the server that runs wp cron event run --due-now every few minutes.
Maintenance
-
wp maintenance-mode activatePut the site in maintenance mode, where visitors see a message that the site is temporarily unavailable.
-
wp maintenance-mode deactivateReturn the site to normal operation. The command also helps when a site is stuck in maintenance mode after an interrupted update.
-
wp maintenance-mode statusShow whether the site is currently in maintenance mode.
Troubleshooting
-
wp plugin list --skip-plugins --skip-themesRun the command without loading the plugins and themes. The options work with every command and let you work even when a plugin or theme causes a critical error.
-
wp plugin deactivate plugin-name --skip-pluginsDeactivate a plugin that causes a critical error, even when the WordPress admin area is not accessible.
-
wp plugin deactivate --allDeactivate all plugins to check whether one of them causes the problem. Caution: the site may lose essential functions, so before you deactivate them, save the list of active plugins so you can reactivate them.
-
wp theme activate theme-name --skip-themesReplace a theme that causes a critical error with another theme, without loading the broken theme.
-
wp plugin list --debugShow detailed information about how WP-CLI and WordPress load, which helps you find the stage where the command fails.
-
wp eval "echo WP_CONTENT_DIR;"Run a short piece of PHP code in the WordPress environment, for example to check the value of a constant. Caution: the code runs with full access to the site and the database, so run only code you know.
-
find . -type f -perm -o+wA shell command (not a WP-CLI command) that finds files in the site directory that any user on the server can write to. Permissions like these are a security risk, so fix them.
-
find . -type d -exec chmod 755 {} \; && find . -type f -exec chmod 644 {} \;A shell command (not a WP-CLI command) that sets the common permissions for WordPress: 755 for directories and 644 for files. Run it only from inside the site directory, and note that some servers need different permissions.
-
chmod 640 wp-config.phpA shell command (not a WP-CLI command) that restricts access to the wp-config.php file, which contains the database password. The right permission depends on the server configuration, and some servers need 600 or 644.
No matching commands.
WP-CLI is the official command-line tool for WordPress, and it lets you do almost anything that is available in the WordPress admin area, without a browser. You run the commands from the site directory over SSH, or from any other directory with the --path option. This way you can combine recurring tasks in one script and manage the site even when the WordPress admin area is not accessible.
You can also change settings in wp-config.php with the wp config set command, without editing the file by hand. To create security keys for manual editing, use the WordPress salt key generator, and to create a strong password for an admin user, use the password generator.
Two habits prevent many problems. Before an update or an import, export a backup of the database with the wp db export command, and test an update of all plugins first with the --dry-run option or on a development site.
WP-CLI commands FAQ
How do I check whether WP-CLI is installed?
The quick way is to run the wp --info command, which shows the details of the tool and the environment when WP-CLI is installed. A message such as command not found means that the tool is not installed or is not in the system search path.
How do I change the domain or move from HTTP to HTTPS?
You change the address with the wp search-replace command, which updates the address in all the tables and keeps data in serialized format intact. First export a backup with the wp db export command, then run the replacement with --dry-run to see the number of changes, and only then run it for real. When you finish, run wp cache flush so the old address is not served from the cache.
How do I reset an admin password when I cannot access the WordPress admin area?
You reset the password with the wp user update admin --prompt=user_pass command, which asks you to type a new password and does not save it in the command history. When you do not know the username, the wp user list --role=administrator command lists all the administrators. After the reset, you can end all active sessions with the wp user session destroy admin --all command.
What is the difference between wp plugin delete and wp plugin uninstall?
The difference is how the plugin data is handled. The wp plugin delete command deletes only the plugin files, and its settings and tables stay in the database. The wp plugin uninstall command runs the uninstall routine defined in the plugin, which may also delete the data, and then deletes the files.
Why do I get an error when I run WP-CLI as the root user?
WP-CLI blocks running as the root user, because when you run it this way, the code of every plugin and theme runs with full privileges, and new files are created with the wrong ownership. The recommended way is to run the commands as the user who owns the site files, for example with sudo -u. The --allow-root option bypasses the block and is meant only for cases where there is no alternative.
What do I do when the site shows a critical error after an update?
You can find and deactivate the plugin that causes the problem from the command line, even when WordPress itself does not load. The wp plugin list --skip-plugins --skip-themes command lists all plugins with the status of each one, and the wp plugin deactivate plugin-name --skip-plugins command deactivates the plugin that was updated last. When the site is stuck in maintenance mode, the wp maintenance-mode deactivate command returns it to normal operation.