Blog

Managing WordPress using command line interface – WP CLI

Tags: command line interfacewordpress

Published on: December 3, 2018 by Daniel Wren

Managing WordPress using command line interface – WP CLI

Scenario:

WP-CLI is the command line interface for WordPress. It is possible to create a wp-admin user, install and update plugins, create posts, change themes etc., with the help of this powerful command line utility. For all the Linux geeks who prefer command line for everything, this comes as the best method to administer a WordPress site.

Just type wp in the command line to list all the commands available. To view the complete documentation you could use wp help <command>. Whenever you are using the wp-cli ensure that you are currently in the WordPress installation directory, else you will encounter an error: No WordPress installation found.

Installing wp-cli on a server :

Before installing wp-cli please ensure that the site is hosted preferably on a UNIX environment with PHP version 5.6 or above and WordPress version 3.7 and above. The steps to install are:

1. Download the Phar file and check whether it works :

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

php wp-cli.phar –info

2. To use wp commands directly, we need to change the permissions and place the file somewhere in your path:

chmod +x wp-cli.phar

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

3. You can check the installation using wp –info which will provide the details of wp-cli installed. If you require to updatewp-cli you can use wp cli update.

The most common situations when you can make use of this are illustrated below:

1. Create a WordPress user from the command line:

wp user create <username> <username>@<domain name> –role=administrator

The possible values for the role include ‘administrator’, ‘editor’, ‘author’, ‘contributor’, ‘subscriber’.

2. Install and activate a plugin or theme from the command line:

wp plugin install <Plugin name> —activate

wp theme install <Theme name> –activate

3. Create a WordPress post from the command line:

wp post create –post_type=page –post_status=publish –post_title=’My test post’ –post_content=’This is a test post’

4. Delete a WordPress post:

wp post delete <post number>

5. Create a backup of the WordPress Database as a .sql file:

wp db export

6. Updating the site URL and home URL in the even of domain name change:

wp option update home https://test.com

wp option update site URL https://test.com

7. Updating WordPress Plugins:

wp plugin update <Plugin name>

8. Upgrading your WordPress version:

wp core update

9. Delete a WordPress user:

wp user delete <user name>

On multisite, wp user delete only removes the user from the current site. Add —network at the end of the previous command to remove the user from the database.

CAUTION: Before deleting a user, you will have to reassign the post to another user, else the posts associated with that user will also get deleted. It can be done as below: 

wp user delete user1 –reassign=user2

To get to know more about installing and using wp-cli please visit the project homepage at https://wp-cli.org/

To get more clarity on the usage of WordPress commands please visit: https://developer.wordpress.org/cli/commands

Build your own team with our dedicated support team.

Category : Linux, WordPress

Daniel Wren

Daniel Wren

An innovative and dedicated IT professional who is very curious to solve and find solutions to seemingly difficult tasks.

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend