WP-CLI is the command-line interface for WordPress which enables you to manage and maintain WordPress sites without using a web browser. There are commands to install plugins, activate theme, update WordPress core, search and replace WordPress database, take a backup and the list goes on. WP CLI is a handy resource for the developers and it is also an effective tool for fixing a hacked WordPress site.

So let’s start with installing WP CLI.

Table of Contents

  1. WP CLI Installation Guide
  2. WP CLI Commands — Usage Examples and Listing
    1. Cache
    2. Cap
    3. CLI
    4. Comment
    5. Config
    6. Core
    7. Cron
    8. Database
    9. Embeds
    10. Language
    11. Maintenance Mode
    12. Media
    13. Menu
    14. Network
    15. Option
    16. Package
    17. Plugin
    18. Post
    19. Post Type
    20. Profile
    21. Rewrite
    22. Role
    23. Scaffold
    24. Sidebar
    25. Site
    26. Super Admin
    27. Taxonomy
    28. Term
    29. Theme
    30. Transient
    31. User
    32. Widget
    33. Internationalization
    34. Others
    35. See Also:

WP CLI Installation Guide

Prerequisites:

  • UNIX-like environment (Linux, FreeBSD, OS X) (limited support in Windows environment)
  • PHP 5.6 or later
  • WordPress 3.7 or later (versions older than the latest WordPress release may have degraded functionality)

Step One: Download the wp-cli.phar file using curl:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Step Two: Verify that the PHAR file is working:

php wp-cli.phar --info

Step Three:  Make wp-cli.phar executable:

chmod +x wp-cli.phar

Step Four: Rename wp-cli.phar to call is wp for ease of use

sudo mv wp-cli.phar wp

Step Five: Verify that wp command is working

wp --info

If WP-CLI was installed successfully, you will see the following output:

OS:	Linux e43.ehosts.com 3.10.0-693.11.6.1.ELK.el6.x86_64 #1 SMP Tue Jan 23 10:30:30 MST 2018 x86_64
Shell:	/usr/local/cpanel/bin/jailshell
PHP binary:	/opt/cpanel/ea-php56/root/usr/bin/php
PHP version:	5.6.40
php.ini used:	/opt/cpanel/ea-php56/root/etc/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/home2/me/public_html/cgi-bin
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	/home2/me/public_html/wp-cli.yml
WP-CLI version:	2.0.0

Reference: Official WP CLI Installation Guide

WP CLI Commands — Usage Examples and Listing

Here are some of the WP CLI commands that I use most of time:

  1. Backing up WordPress Database:wp db export example-db.sqlreplace example-db.sql with what you want to call your sql backup file
  2. Database Search and Replace:wp search-replace ‘http://example.test’ ‘http://example.com’replace http://example.test with old value and http://example.com with new value
  3. Flush cache:wp cache flush
  4. Install and activate plugin:wp plugin install bbpress –activate
  5. Flush / Refresh Permalink:wp rewrite flush

And here’s a complete list of WP CLI commands for managing WordPress via command-line interface:

Cache

wp cache commands for adding, removing, fetching, and flushing the WP Object Cache object

CommandDescription
wp cache addAdds a value to the object cache
wp cache decrDecrements a value in the object cache
wp cache deleteRemoves a value from the object cache
wp cache flushFlushes the object cache
wp cache getGets a value from the object cache
wp cache incrIncrements a value in the object cache
wp cache replaceReplaces a value in the object cache, if the value already exists
wp cache setSets a value to the object cache, regardless of whether it already exists
wp cache typeAttempts to determine which object cache is being used

Cap

wp cap commands for adding, removing, and listing capabilities of a user role

CommandDescription
wp cap addAdds capabilities to a given role
wp cap listLists capabilities for a given role
wp cap removeRemoves capabilities from a given role

CLI

wp cli commands for reviewing current WP-CLI info, checking for updates, or viewing defined aliases

CommandDescription
wp cli aliasRetrieves, sets and updates aliases for WordPress Installations
wp cli cacheManages the internal WP-CLI cache
wp cli check-updateChecks to see if there is a newer version of WP-CLI available
wp cli cmd-dumpDumps the list of installed commands, as JSON
wp cli completionsGenerates tab completion strings
wp cli has-commandDetects if a command exists
wp cli infoPrints various details about the WP-CLI environment
wp cli param-dumpDumps the list of global parameters, as JSON or in var_export format
wp cli updateUpdates WP-CLI to the latest release
wp cli versionPrints WP-CLI version

Comment

wp comment commands for creating, updating, deleting, and moderating comments

CommandDescription
wp comment approveApproves a comment
wp comment countCounts comments, on whole blog or on a given post
wp comment createCreates a new comment
wp comment deleteDeletes a comment
wp comment existsVerifies whether a comment exists
wp comment generateGenerates some number of new dummy comments
wp comment getGets the data of a single comment
wp comment listGets a list of comments
wp comment metaAdds, updates, deletes, and lists comment custom fields
wp comment recountRecalculates the comment_count value for one or more posts
wp comment spamMarks a comment as spam
wp comment statusGets the status of a comment
wp comment trashTrashes a comment
wp comment unapproveUnapproves a comment
wp comment unspamUnmarks a comment as spam
wp comment untrashUntrashes a comment
wp comment updateUpdates one or more comments

Config

wp config commands for generating and reading the wp-config.php file

CommandDescription
wp config createGenerates a wp-config.php file
wp config deleteDeletes a specific constant or variable from the wp-config.php file
wp config editLaunches system editor to edit the wp-config.php file
wp config getGets the value of a specific constant or variable defined in wp-config.php file
wp config hasChecks whether a specific constant or variable exists in the wp-config.php file
wp config listLists variables, constants, and file includes defined in wp-config.php file
wp config pathGets the path to wp-config.php file
wp config setSets the value of a specific constant or variable defined in wp-config.php file
wp config shuffle-saltsRefreshes the salts defined in the wp-config.php file

Core

wp core commands for downloading, installing, updating, and managing a WordPress installation

CommandDescription
wp core check-updateChecks for WordPress updates via Version Check API
wp core downloadDownloads core WordPress files
wp core installRuns the standard WordPress installation process
wp core is-installedChecks if WordPress is installed
wp core multisite-convertTransforms an existing single-site installation into a multisite installation
wp core multisite-installInstalls WordPress multisite from scratch
wp core updateUpdates WordPress to a newer version
wp core update-dbRuns the WordPress database update procedure
wp core verify-checksumsVerifies WordPress files against WordPress.org’s checksums
wp core versionDisplays the WordPress version

Cron

wp cron commands for testing, runing, and deleting WP-Cron events; managing WP-Cron schedules

CommandDescription
wp cron eventSchedules, runs, and deletes WP-Cron events
wp cron scheduleGets WP-Cron schedules
wp cron testTests the WP Cron spawning system and reports back its status

Database

wp db commands for performing basic database operations using credentials stored in wp-config.php

CommandDescription
wp db checkChecks the current status of the database
wp db cleanRemoves all tables with `$table_prefix` from the database
wp db cliOpens a MySQL console using credentials from wp-config.php
wp db columnsDisplays information about a given table
wp db createCreates a new database
wp db dropDeletes the existing database
wp db exportExports the database to a file or to STDOUT
wp db importImports a database from a file or from STDIN
wp db optimizeOptimizes the database
wp db prefixDisplays the database table prefix
wp db queryExecutes a SQL query against the database
wp db repairRepairs the database
wp db resetRemoves all tables from the database
wp db searchFinds a string in the database
wp db sizeDisplays the database name and size
wp db tablesLists the database tables

Embeds

wp embed commands for inspecting oEmbed providers, clearing embed cache, and more

CommandDescription
wp embed cacheFinds, triggers, and deletes oEmbed caches
wp embed fetchAttempts to convert a URL into embed HTML
wp embed handlerRetrieves embed handlers
wp embed providerRetrieves oEmbed providers

Language

wp language commands for installing, activating, and managing language packs

CommandDescription
wp language coreInstalls, activates, and manages core language packs
wp language pluginInstalls, activates, and manages plugin language packs
wp language themeInstalls, activates, and manages theme language packs

Maintenance Mode

wp maintenance-mode commands for activating, deactivating or checking the status of the maintenance mode of a site

CommandDescription
wp maintenance-mode activateActivates maintenance mode
wp maintenance-mode deactivateDeactivates maintenance mode
wp maintenance-mode is-activeDetects maintenance mode status
wp maintenance-mode statusDisplays maintenance mode status

Media

wp media commands for importing files as attachments, regenerating thumbnails, or listing registered image sizes

CommandDescription
wp media fix-orientationFix image orientation for one or more attachments
wp media image-sizeLists image sizes registered with WordPress
wp media importCreates attachments from local files or URLs
wp media regenerateRegenerates thumbnails for one or more attachments

wp menu commands for listing, creating, assigning, and deleting the active theme’s navigation menus

CommandDescription
wp menu createCreates a new menu
wp menu deleteDeletes one or more menus
wp menu itemList, add, and delete items associated with a menu
wp menu listGets a list of menus
wp menu locationAssigns, removes, and lists a menu’s locations

Network

wp network commands for performing network-wide operations

CommandDescription
wp network metaGets, adds, updates, deletes, and lists network custom fields.
wp network meta addAdd a meta field
wp network meta deleteDelete a meta field
wp network meta getGet meta field value
wp network meta listList all metadata associated with an object
wp network meta patchUpdate a nested value for a meta field
wp network meta pluckGet a nested value from a meta field
wp network meta updateUpdate a meta field

Option

wp option commands for retrieving and setting site options, including plugin and WordPress settings

CommandDescription
wp option addAdds a new option value
wp option deleteDeletes an option
wp option getGets the value for an option
wp option listLists options and their values
wp option patchUpdates a nested value in an option
wp option pluckGets a nested value from an option
wp option updateUpdates an option value

Package

wp package commands for listing, installing, and removing WP-CLI packages

CommandDescription
wp package browseBrowses WP-CLI packages available for installation
wp package installInstalls a WP-CLI package
wp package listLists installed WP-CLI packages
wp package pathGets the path to an installed WP-CLI package, or the package directory
wp package uninstallUninstalls a WP-CLI package
wp package updateUpdates all installed WP-CLI packages to their latest version

Plugin

wp plugin commands for managing plugins, including installs, activations, and updates

CommandDescription
wp plugin activateActivates one or more plugins
wp plugin auto-updatesManages plugin auto-updates
wp plugin deactivateDeactivates one or more plugins
wp plugin deleteDeletes plugin files without deactivating or uninstalling
wp plugin getGets details about an installed plugin
wp plugin installInstalls one or more plugins
wp plugin is-activeChecks if a given plugin is active
wp plugin is-installedChecks if a given plugin is installed
wp plugin listGets a list of plugins
wp plugin pathGets the path to a plugin or to the plugin directory
wp plugin searchSearches the WordPress.org plugin directory
wp plugin statusReveals the status of one or all plugins
wp plugin toggleToggles a plugin’s activation state
wp plugin uninstallUninstalls one or more plugins
wp plugin updateUpdates one or more plugins
wp plugin verify-checksumsVerifies plugin files against WordPress.org’s checksums

Post

wp post commands for managing posts, content, and meta

CommandDescription
wp post createCreates a new post
wp post deleteDeletes an existing post
wp post editLaunches system editor to edit post content
wp post existsVerifies whether a post exists
wp post generateGenerates some posts
wp post getGets details about a post
wp post listGets a list of posts
wp post metaAdds, updates, deletes, and lists post custom fields
wp post termAdds, updates, removes, and lists post terms
wp post updateUpdates one or more existing posts

Post Type

wp post-type commands for retrieving details on the site’s registered post types

CommandDescription
wp post-type getGets details about a registered post type
wp post-type listLists registered post types

Profile

wp profile commands

CommandDescription
wp profile evalProfile arbitrary code execution
wp profile eval-fileProfile execution of an arbitrary file
wp profile hookProfile key metrics for WordPress hooks (actions and filters)
wp profile stageProfile each stage of the WordPress load process (bootstrap, main_query, template)

Rewrite

wp profile commands for listing or flushing the site’s rewrite rules, updating the permalink structure

CommandDescription
wp rewrite flushFlushes rewrite rules
wp rewrite listGets a list of the current rewrite rules
wp rewrite structureUpdates the permalink structure

Role

wp role commands for managing user roles, including creating new roles and resetting to defaults

CommandDescription
wp role createCreates a new role
wp role deleteDeletes an existing role
wp role existsChecks if a role exists
wp role listLists all roles
wp role resetResets any default role to default capabilities

Scaffold

wp scaffold commands for generating code for post types, taxonomies, plugins, child themes, etc

CommandDescription
wp scaffold blockGenerates PHP, JS and CSS code for registering a Gutenberg block for a plugin or theme
wp scaffold child-themeGenerates child theme based on an existing theme
wp scaffold pluginGenerates starter code for a plugin
wp scaffold plugin-testsGenerates files needed for running PHPUnit tests in a plugin
wp scaffold post-typeGenerates PHP code for registering a custom post type
wp scaffold taxonomyGenerates PHP code for registering a custom taxonomy
wp scaffold theme-testsGenerates files needed for running PHPUnit tests in a theme
wp scaffold underscoresGenerates starter code for a theme based on _s
wp scaffold _sGenerates starter code for a theme based on _s

wp sidebar commands for listing registered sidebars

CommandDescription
wp sidebar listLists registered sidebars

Site

wp site commands for creating, deleting, emptying, moderating, and listing one or more sites on a multisite installation

CommandDescription
wp site activateActivates one or more sites
wp site archiveArchives one or more sites
wp site createCreates a site in a multisite installation
wp site deactivateDeactivates one or more sites
wp site deleteDeletes a site in a multisite installation
wp site emptyEmpties a site of its content (posts, comments, terms, and meta)
wp site listLists all sites in a multisite installation
wp site matureSets one or more sites as mature
wp site metaAdds, updates, deletes, and lists site custom fields
wp site optionAdds, updates, deletes, and lists site options in a multisite installation
wp site privateSets one or more sites as private
wp site publicSets one or more sites as public
wp site spamMarks one or more sites as spam
wp site switch-languageActivates a given language
wp site unarchiveUnarchives one or more sites
wp site unmatureSets one or more sites as immature
wp site unspamRemoves one or more sites from spam

Super Admin

wp super-admin commands for listing, adding, or removing super admin users on a multisite installation

CommandDescription
wp super-admin addGrants super admin privileges to one or more users
wp super-admin listLists users with super admin capabilities
wp super-admin removeRemoves super admin privileges from one or more users

Taxonomy

wp taxonomy commands for retrieving information about registered taxonomies

CommandDescription
wp taxonomy getGets details about a registered taxonomy
wp taxonomy listLists registered taxonomies

Term

wp term commands for managing taxonomy terms and term meta, with create, delete, and list commands

CommandDescription
wp term createCreates a new term
wp term deleteDeletes an existing term
wp term generateGenerates some terms
wp term getGets details about a term
wp term listLists terms in a taxonomy
wp term metaAdds, updates, deletes, and lists term custom fields
wp term migrateMigrate a term of a taxonomy to another taxonomy
wp term recountRecalculates number of posts assigned to each term
wp term updateUpdates an existing term

Theme

wp theme commands for managing themes, including installs, activations, and updates

CommandDescription
wp theme activateActivates a theme
wp theme auto-updatesManages theme auto-updates
wp theme deleteDeletes one or more themes
wp theme disableDisables a theme on a WordPress multisite install
wp theme enableEnables a theme on a WordPress multisite install
wp theme getGets details about a theme
wp theme installInstalls one or more themes
wp theme is-activeChecks if a given theme is active
wp theme is-installedChecks if a given theme is installed
wp theme listGets a list of themes
wp theme modSets, gets, and removes theme mods
wp theme pathGets the path to a theme or to the theme directory
wp theme searchSearches the WordPress.org theme directory
wp theme statusReveals the status of one or all themes
wp theme updateUpdates one or more themes

Transient

wp transient commands for adding, getting, and deleting entries in the WordPress Transient Cache

CommandDescription
wp transient deleteDeletes a transient value
wp transient getGets a transient value
wp transient listLists transients and their values
wp transient setSets a transient value
wp transient typeDetermines the type of transients implementation

User

wp user commands for managing users, along with their roles, capabilities, and meta

CommandDescription
wp user add-capAdds a capability to a user
wp user add-roleAdds a role for a user
wp user application-passwordCreates, updates, deletes, lists and retrieves application passwords
wp user check-passwordChecks if a user’s password is valid or not
wp user createCreates a new user
wp user deleteDeletes one or more users from the current site
wp user generateGenerates some users
wp user getGets details about a user
wp user import-csvImports users from a CSV file
wp user listLists users
wp user list-capsLists all capabilities for a user
wp user metaAdds, updates, deletes, and lists user custom fields
wp user remove-capRemoves a user’s capability
wp user remove-roleRemoves a user’s role
wp user reset-passwordResets the password for one or more users
wp user sessionDestroys and lists a user’s sessions
wp user set-roleSets the user role
wp user spamMarks one or more users as spam
wp user termAdds, updates, removes, and lists user terms
wp user unspamRemoves one or more users from spam
wp user updateUpdates an existing user

Widget

wp widget commands for managing widgets, including adding and moving them within sidebars

CommandDescription
wp widget addAdds a widget to a sidebar
wp widget deactivateDeactivates one or more widgets from an active sidebar
wp widget deleteDeletes one or more widgets from a sidebar
wp widget listLists widgets associated with a sidebar
wp widget moveMoves the position of a widget
wp widget resetResets sidebar
wp widget updateUpdates options for an existing widget

Internationalization

wp i18n commands for providing internationalization tools for WordPress projects

CommandDescription
wp i18n make-jsonExtract JavaScript strings from PO files and add them to individual JSON files
wp i18n make-moCreate MO files from PO files
wp i18n make-potCreate a POT file for a WordPress project
wp i18n update-poUpdate PO files from a POT file

Others

WP CLI commands which do not have any subcommands

CommandDescription
wp adminOpen /wp-admin/ in a browser
wp evalExports WordPress content to a WXR file
wp eval-fileLoads and executes a PHP file
wp importImports content from a given WXR file
wp exportExports WordPress content to a WXR file
wp findFind WordPress installations on the filesystem
wp helpGets help on WP-CLI, or on a specific command
wp shellOpens an interactive PHP console for running and testing PHP code
wp serverLaunches PHP’s built-in web server for a specific WordPress installation
wp search-replaceSearches/replaces strings in the database
wp dist-archiveCreate a distribution archive based on a project’s .distignore file

See Also:

This article is written by Evelyn Allison. Evelyn has over two decades of experience with the big-tech corporate giants. Starting in 2002 with consumer IT remote support, he transitioned into IT enterprise support and systems provisioning for Windows and Linux servers. Her prowess spans her expertise in network security, security audit and scripting-based-automation. Actively involved in web security since 2017, Evelyn has worked with various technologies to secure the web, leveraging tech like Nginx, modsecurity, reverse-proxies, developing web-application-firewalls, on-the-fly asset optimization using Google’s PageSpeed Module and more. Her expertise is reflected in the top-tier plugins and comprehensive consulting-services she offers in the domain of web-security.

Need help with restoring your hacked site?

Our dedicated incident response team, state-of-the-art technology, and excellent customer service makes sure that your website is 100% clean and secure.

Request Malware Cleanup →