WP CLI: Most Useful Commands

Harit Panchal
4 min readApr 29, 2022
WP CLI Usefull Commands

The terminal is a program that you use to type in commands. It is a text input/output environment and is most admired by developers around the world.

WordPress also has its command-line interface for many actions you might perform in the WordPress admin using WP-CLI.

This blog will cover the basics of WP-CLI and the most useful commands you can use to speed up the development process so let's dive in.

What is WP-CLI?

WP-CLI is a command-line tool for developers to manage common tasks (and not so common) of a WordPress installation. It can add/remove users, posts, and categories, insert test data, search and replace in the database, reset passwords, help troubleshoot performance issues, and much more!

Installing WP-CLI

Three steps to install WP-CLI in your system (macOS, Linux, FreeBSD, Cygwin).

Step-1

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

Step-2

chmod +x wp-cli.phar

Step-3

sudo mv wp-cli.phar /usr/local/bin

WP-CLI Commands

And now the main part, commands.

wp admin

wp admin: This command will open /wp-admin/ in the web browser. Very simple way.wp admin --url=http://test.localhost.en/: With help of  the —url  parameter you will be able to open /wp-admin/ for a particular subsite(in this case: http://test.localhost.en/)  of the multisite setup.

wp cap

wp cap list (user-role): Lists all capabilities for user role.wp cap add (user-role) (capability): Add capability to the user role.wp cap remove (user-role) (capability): Removes capability from the user role.
wp cap example
wp role example

wp core

wp core download: Downloads the latest version of WordPress core in 2 seconds(based on your internet speed 😬).wp config create --dbname=auto_dep --dbuser=admin --dbpass=YOUR_PASSWORD: Creates config file in thesetup.

(wp-config is a different command but documentation shows it before wp core and it’s confusing.)

wp core install --url=SITE_URL --title=SITE_TITLE --admin_user=USER_NAME --admin_email=YOUR_EMAIL --admin_password=YOUR_PASSWORD: Installs WP.wp core multisite-convert: Converts into multisite.wp core update: Updates core to the latest version.

wp embed

wp embed fetch (URL): Retrieves embed URL for any link

(Works for the supported providers in WP oEmbed class like Youtube, Twitter, Dailymotion, Spotify, Amazon, TikTok, Pinterest. Instagram)

Example:

wp embed fetch https://www.youtube.com/watch?v=ZcOmw4rDCQU
Returns iframe for youtube video
<iframe title=”Cristiano Ronaldo Celebration | Loudest Crowd Roar!!” width=”500" height=”281" src=”https://www.youtube.com/embed/ZcOmw4rDCQU?feature=oembed" frameborder=”0" allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture” allowfullscreen></iframe>

wp eval

wp eval (code): Echoes the output in the terminal.

Example:

wp eval ‘echo rand(1,10);’: Echoes random digit between 1 to 10.wp eval ‘echo WP_CONTENT_DIR;’: Echoes WP Content Directorywp eval ‘echo get_template_directory();’: Echoes template directory.

wp help

wp help (command): Get help on a specific command.

Example:

# get help for `core` command
wp help core

# get help for `core download` subcommand
wp help core download

wp maintenance-mode

wp maintenance-mode activate: Activates maintenance mode.wp maintenance-mode deactivate: Deactivates maintenance mode.

wp option

wp option list: Gets all options from the site.wp option add (option-name) foobar: Adds new option(option-name) to the database.

wp plugin

wp plugin install (plugin slug): Installs plugin from WP plugin directory.wp plugin activate (plugin slug) (plugin slug): Activates multiple plugins.wp plugin search (plugin name): Search the plugin in the WP plugin directory.
wp plugin example
wp plugin example

wp post

wp post generate --count=10 --post_type=(post-slug): Generates 10 posts in (post-slug) post type.wp post delete $(wp post list --post_type=(post-slug) --format=ids)--force: Deletes all posts of (post-slug) post type.wp post update (ids) --post_content=’(any content’): Updates content of multiple posts.

wp scaffold (one of the best)

wp scaffold plugin (plugin slug): Creates starter code for the plugin.wp scaffold child-theme (child-theme-domain)  — parent_theme=(parent-theme-domain): Create starter code for the child theme.wp scaffold post-type (post-slug) --label=”(post type label)” --dashicon=”(dashicon)” --theme=(theme-domain): Create starter code for post-type in theme.

wp search-replace

wp search-replace ‘(old-url)' ‘(new-url)' :Finds and replaces old-url with new-url in the database (bit risky).

That's all, these were some of the commands which I find useful for day-to-day development.

Create Your Custom WP-CLI Command

You can also create your command according to requirements and use it accordingly.

Here is the code snippet for the same, try it:


class WDS_CLI {
public function hello_world() { WP_CLI::line( 'Hello World!' ); $rand = rand(1,2); if($rand == 1){ WP_CLI::success( 'Success!' ); } else{ WP_CLI::error( 'Error!' ); } }}function wds_cli_register_commands() { WP_CLI::add_command( 'wds', 'WDS_CLI' );}add_action( 'cli_init', 'wds_cli_register_commands' );

Conclusion

In short, WP-CLI is the best way to fasten development. WP-CLI is indeed a powerful tool that can be used to manage your WordPress sites through the command line. These commands come with various parameters and subcommands and you can manage your database, take backups, and WordPress multisite. You can do so many things with it🔥.

Check out more on WP-CLI.

By Harit Panchal

--

--