WP-CLI commands cheat sheet

This cheat sheet brings together the most useful WP-CLI commands for managing a WordPress site from the command line, from updating the core and plugins to backing up the database and changing the site address, with a short explanation of each command. You can search for a command, filter by section, and copy it with one click.

Getting started and info

  • wp --info

    Show 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 version

    Show the installed version of WP-CLI.

  • wp help

    List the available commands with a short description of each one.

  • wp help plugin install

    Show 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_html

    List 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.com

    Show 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 list

    Run 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-root

    Allow 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 version

    Show the installed version of WordPress.

  • wp core check-update

    Check whether a new version of WordPress is available and list the available updates, without installing them.

  • wp core update

    Update WordPress to the latest version. Before you update, back up the files and the database.

  • wp core update --minor

    Install only maintenance and security updates within the current version, without moving to a new major version.

  • wp core update-db

    Run 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_US

    Download 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=dbpass

    Create 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_password

    Complete 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-checksums

    Compare 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 --activate

    Install 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 list

    List all installed plugins, including the status, the version, and whether an update is available.

  • wp plugin list --update=available

    List only the plugins that have an update available.

  • wp plugin list --status=active --field=name

    List only the names of the active plugins, one line per plugin. This output is convenient for scripts.

  • wp plugin install plugin-name --activate

    Install a plugin from the official directory by its slug and activate it right away.

  • wp plugin install ./plugin-name.zip --activate

    Install 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-name

    Activate an installed plugin.

  • wp plugin deactivate plugin-name

    Deactivate a plugin without deleting its files or settings.

  • wp plugin status plugin-name

    Show the plugin details, including the status, the version, the author, and the description.

  • wp plugin update --all --dry-run

    Show which plugins would be updated and to which version, without actually updating them.

  • wp plugin update --all

    Update all plugins that have an update available. Back up the site before you update, and check the main pages afterward.

  • wp plugin verify-checksums --all

    Compare 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-name

    Delete 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 --deactivate

    Deactivate 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 list

    List all installed themes, including the active theme and the available updates.

  • wp theme install theme-name

    Install a theme from the official directory by its slug, without activating it.

  • wp theme install ./theme-name.zip --activate

    Install a theme from a local ZIP file and activate it right away.

  • wp theme activate theme-name

    Switch the active theme of the site.

  • wp theme update --all

    Update 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-name

    Delete 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 list

    List all the users of the site, including the ID, the username, the email address, and the role.

  • wp user list --role=administrator

    List 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=editor

    Create 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_pass

    Change 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-password

    Generate 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 author

    Replace a user's role with another role and remove the user's previous roles.

  • wp user add-role username editor

    Add another role to a user, without removing the user's existing roles.

  • wp user session destroy admin --all

    Log 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=1

    Delete 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.sql

    Export 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.gz

    Export the database and compress it straight into a gz file, without creating an uncompressed intermediate file.

  • wp db import backup.sql

    Import 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 check

    Check the tables in the database and report damaged tables.

  • wp db repair

    Try 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 optimize

    Optimize 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-readable

    Show the total size of the database in readable units, such as MB.

  • wp db size --tables --human-readable

    Show 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-run

    Show 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=guid

    Run 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-tables

    Run 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" --precise

    Process 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 --yes

    Caution: 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 siteurl

    Show the address where the WordPress files are installed.

  • wp option get home

    Show 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 0

    Ask 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 list

    List 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_DEBUG

    Show the value of a specific constant in the wp-config.php file.

  • wp config set WP_DEBUG true --raw

    Turn 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 --raw

    Make 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 --raw

    Turn off the plugin and theme file editor in the WordPress admin area, so nobody can edit code through the browser.

  • wp config shuffle-salts

    Replace 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_status

    List the pages of the site with the ID, title, and status of each page.

  • wp post delete 123

    Move a post to the trash. You can restore it from the trash in the WordPress admin area.

  • wp post delete 123 --force

    Caution: this deletes the post permanently, without moving it to the trash.

  • wp post delete $(wp post list --post_status=trash --format=ids) --force

    Caution: 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) --force

    Delete all the revisions of posts and pages. Caution: after the deletion, you cannot restore an earlier version of the content.

  • wp post generate --count=10

    Create 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) --force

    Caution: this permanently deletes all the comments marked as spam, without moving them to the trash.

  • wp media regenerate --yes

    Regenerate 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 --yes

    Create only the missing image sizes, without regenerating existing sizes, so it runs faster.

  • wp export --dir=./export

    Export 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 flush

    Flush 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 --expired

    Delete only the expired transients.

  • wp transient delete --all

    Delete all transients. WordPress and the plugins recreate them as needed, so the first load after the deletion may be slower.

  • wp rewrite flush

    Rebuild 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 list

    List all the WordPress scheduled tasks and the next run time of each task.

  • wp cron event run --due-now

    Immediately run all the tasks whose scheduled time has arrived.

  • wp cron test

    Check whether WordPress cron can run on the site and report problems.

  • wp config set DISABLE_WP_CRON true --raw

    Stop 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 activate

    Put the site in maintenance mode, where visitors see a message that the site is temporarily unavailable.

  • wp maintenance-mode deactivate

    Return the site to normal operation. The command also helps when a site is stuck in maintenance mode after an interrupted update.

  • wp maintenance-mode status

    Show whether the site is currently in maintenance mode.

Troubleshooting

  • wp plugin list --skip-plugins --skip-themes

    Run 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-plugins

    Deactivate a plugin that causes a critical error, even when the WordPress admin area is not accessible.

  • wp plugin deactivate --all

    Deactivate 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-themes

    Replace a theme that causes a critical error with another theme, without loading the broken theme.

  • wp plugin list --debug

    Show 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+w

    A 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.php

    A 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.

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.

Did you know?

A WordPress VPS comes with full root access over SSH.

WordPress VPS plans

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.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement