Managing WordPress Sites Using WP-CLI

Managing WordPress sites sits at the core of the services we provide and boy do we do a lot of it. We troubleshoot, interact with, fix, manage, update, and look at many sites everyday, both as individuals and as a team. Having done this for several years now, we’ve managed to pick up tricks and find tools that help make sites dramatically easier to manage.

We spend so much time on the command line when we are interacting with sites that it can become very cumbersome to switch between command line and browser to accomplish simple tasks that require separate interfaces. Because of this, staying in the command line for as many tasks as possible is vital to maximizing productivity and efficiency.

Although many of these tricks and tools are worth sharing, today we will focus on WP-CLI, a set of command line tools that make managing WordPress sites from the command line a cinch. We will provide some simple use cases that can have a big impact on productivity and maximizing time, but you should read about all of the commands available on the WP-CLI website to find other ways you can fit it into your workflow.

Before anything, you will need do get WP-CLI downloaded and installed. There are several different ways to make this happen, so definitely check out the WP-CLI website for guidance on how to install it for your needs.

Automation

One of the best things about WP-CLI isn’t necessarily the tools that are provided, it is what those tools make possible. On this note, I submit that WP-CLI is an incredibly powerful tool for use in automated management of WordPress sites, especially for developers who manage large amounts of sites. This could be done in any number ways.

Some examples of things you could automate:

  • The installation, removal, activation, and deactivation of a plugin or set of plugins across all sites you manage.
  • Use of a bash script to automate the updating of WordPress core and/or plugins at a frequency you determine.
  • Automate the addition or removal of users across several sites. This could be especially useful to agencies that need to add/remove staff members on a group of sites they manage.
  • Automate a simple malware scan using `wp core verify-checksums`.
  • Automate the download of WordPress, wp-config creation, and initial site setup to quickly create/deploy a site. It is also very easy to use WP-CLI to import a default database if you have a something preconfigured that you want to use.

The especially intriguing one here is the possibility to automate the deploy of an entire website. As a developer, starting up a new project can be a pain and hinder the creative process when you have to spend so much time getting the basic install down. Being able to run a bash script that uses WP-CLI to automate the download, deploy, and setup of a site means that I can go from zero to WordPress in seconds.

Quick and Efficient User Management

One of the struggles of taking over a site or beginning work on an existing site is getting your user situation settled. It isn’t strange to have to talk with several different people and track down email addresses just so you can get logged in to begin work on a site. WP-CLI helps make issues like this a thing of the past because of the user management tools it provides. Not having login information is no longer an issue when you can create a new administrative user via the command line.


$ wp user create mysiteadmin admin@mydomain.com --role=administrator --user_pass=mysecurepassword
Success: Created user 2.

$ wp user list
+----+------------+--------------+-------------------------+---------------------+---------------+
| ID | user_login | display_name | user_email | user_registered | roles |
+----+------------+--------------+-------------------------+---------------------+---------------+
| 1 | iamrobertv | iamrobertv | robert@mydomain.com | 2014-03-03 23:17:05 | administrator |
| 2 | mysiteadmin | mysiteadmin | admin@mydomain.com | 2014-10-15 12:10:06 | administrator |
+----+------------+--------------+-------------------------+---------------------+---------------+

The ability to list all users on a site and display their important information is quite useful as well, especially when somebody needs a password reset or an email address changed.


$ wp user update 2 --user_pass=mynewermoresecurepassword
Success: Updated user 2.

$ wp user update 2 --user_email=newemail@mydomain.com
Success: Updated user 2.

$ wp user list
+----+------------+--------------+-------------------------+---------------------+---------------+
| ID | user_login | display_name | user_email | user_registered | roles |
+----+------------+--------------+-------------------------+---------------------+---------------+
| 1 | iamrobertv | iamrobertv | robert@mydomain.com | 2014-03-03 23:17:05 | administrator |
| 2 | mysiteadmin | mysiteadmin | newemail@mydomain.com | 2014-10-15 12:10:06 | administrator |
+----+------------+--------------+-------------------------+---------------------+---------------+

The user management tools are very power and even offer the ability to delete users, manage roles, get all information for a user, and even generate dummy users for testing purposes.

WordPress DB Management

During the building and troubleshooting process, it is necessary to keep comprehensive backups of the WordPress database whenever you are testing changes or about to perform major upgrades/updates. The `wp db` command helps make backing up, restoring, and managing the WordPress database easy.


$ wp db export
Success: Exported to mysitedb.sql

$ wp db import newdb.sql
Success: Imported from newdb.sql

There are also options to optimize, repair, and query the database directly from the command line. Additionally, the `wp db cli` subcommand will pop open a mysql shell using the credentials in your wp-config.php with ease.

Plugin and Theme Management

At the most basic level, viewing your current set of plugins is rather simple and there are even parameters to pass to show only active, inactive, or must-use plugins:

$ wp plugin status

wp plugin status
6 installed plugins:
A akismet 3.0.2
A debug-bar 0.8.2
A developer 1.2.5
I gravityforms 1.6.3.1.2
I redirection 2.3.6
M batcache

Legend: I = Inactive, A = Active, M = Must Use

You can also use the `plugin` command to install, activate, deactivate, delete, and even search for plugins. The –activate parameter can be passed to a `plugin install` subcommand to activate a plugin upon install. You can also use `plugin activate` if you want to install a plugin and then activate it later.


$ wp plugin install hello-dolly --activate
Installing Hello Dolly (1.6)
Downloading install package from https://downloads.wordpress.org/plugin/hello-dolly.1.6.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Activating 'hello-dolly'...
Success: Plugin 'hello-dolly' activated.

$ wp plugin deactivate hello-dolly
Success: Plugin 'hello-dolly' deactivated.

$ wp plugin delete hello-dolly
Success: Deleted 'hello-dolly' plugin.

$ wp plugin update --all
Success: Updated 4/6 plugins.

You can even search the WordPress plugin repository right from the command line and use the slug you get back to immediately run the install command.


$ wp plugin search redirection
Success: Showing 10 of 1470 plugins.
+---------------------------------+--------------------------------+--------+
| name | slug | rating |
+---------------------------------+--------------------------------+--------+
| Redirection | redirection | 78 |
| Quick Page/Post Redirect Plugin | quick-pagepost-redirect-plugin | 93 |
| Peter's Login Redirect | peters-login-redirect | 97 |
| SEO Redirection Plugin | seo-redirection | 100 |
| jonimo Simple Redirect | jonimo-simple-redirect | 100 |
| Safe Redirect Manager | safe-redirect-manager | 97.6 |
| Eggplant 301 Redirects | eps-301-redirects | 84.8 |
| WP Redirects | wp-redirects | 64 |
| Simple Mobile URL Redirect | simple-mobile-url-redirect | 94.6 |
| Equivalent Mobile Redirect | equivalent-mobile-redirect | 80 |
+---------------------------------+--------------------------------+--------+

The plugin management tools are very powerful and being able to activate/deactivate from the command line helps make troubleshooting issues a breeze. You can even pass this command multiple plugin slugs for bulk management.

Theme management works very much the same.


$ wp theme status

8 installed themes:
A launcheffect 2.02Lite
I pagelines 2.1.3
I platformbase 1.4.1
I platformpro 1.5.3
UI twentyeleven 1.3
UI twentyfourteen 1.0
UI twentythirteen 1.0
UI twentytwelve 1.1

Legend: I = Inactive, A = Active, U = Update Available

$ wp theme activate pagelines
Success: Switched to 'PageLines Framework' theme.

Use `wp theme` to search for, install, delete, update, and manage your themes without ever having to log in to the site.

Serialized Search and Replace

If you build sites in a local environment or on a staging server, you know that running a search replace on URLs in the database becomes necessary before launching. The `wp search-replace` command will run a serialized search and replace on the database to ensure that all URLs are found and replaced properly, including those that are a part of serialized data. You can even use the –dry-run flag to see how many items would be replaced before you actually run it.


$ wp search-replace "dev.mydomain.com" "mydomain.com"
+--------------------------+-----------------------+--------------+
| Table | Column | Replacements |
+--------------------------+-----------------------+--------------+
| iarv_options | option_value | 10 |
| iarv_options | autoload | 0 |
| iarv_postmeta | meta_key | 0 |
| iarv_postmeta | meta_value | 13 |
| iarv_posts | guid | 21 |
| iarv_posts | post_type | 0 |
| iarv_posts | post_mime_type | 0 |
+--------------------------+-----------------------+--------------+
Success: Made 44 replacements.

The data table above is truncated for the sake of example. The actual output displays all tables/columns whether they had replacements or not. Make sure and use a `wp db export` before hand to backup your database, just in case.

Core Management

The `wp core` command offers powerful options for managing the WordPress core files. Setting up a brand new site using WP-CLI is an easy process and can be done without leaving the command line. Using the `wp core download`, `wp core config`, and `wp core install` subcommands.

The `wp core download` subcommand will download the latest version of WordPress into your current directory. The `wp core config` subcommand will take your parameters and generate a wp-config.php file for you using the values you insert. The `wp core install` subcommand will use the generated wp-config.php file and install WordPress using the parameters you pass for the site’s url, title, and admin user information.


$ wp core download

$ wp core config --dbname=mysitedb --dbuser=mydbuser --dbpass=mydbpass --dbhost=localhost --dbprefix=wp_

$ wp core install --url=mywebsite.com --title="My New Site" --admin_user=webadmin --admin_password=adminpass --admin_email="admin@mywebsite.com"

If your WordPress install needs to be updated, you can use the `wp core update` and `wp core update-db` subcommands to update core files and the database (if the database needs to be updated).


$ wp core update
Downloading update from https://downloads.wordpress.org/release/wordpress-4.0-no-content.zip...
Unpacking the update...
Success: WordPress updated successfully.

$ wp core update-db
Success: WordPress database upgraded successfully.

Much Much More

WP-CLI has an incredible amount of options and offers amazing flexibility for taking care of tasks easily. Even all of the commands discussed above have other subcommands that are useful in many different ways. It offers comment management, posting directly from command line, cache management, media management, cron management, and even multisite support. The possibilities are truly incredibly and I highly encourage you to give WP-CLI a shot to improve your efficiency when you drop into command line to manage your sites.

Documentation is available via the WP-CLI website and in the toolset itself. When in doubt, just use the `wp help` command to see what options are available to WP-CLI as a whole and to each individual command and subcommand.

Happy developing!


Premium WordPress Hosting vs. Traditional Hosting: Which is Better?

More and more businesses are turning to premium WordPress hosting in order to optimize their web presence and streamline their business operations, but traditional hosting is still an option. Dive into the pros and cons of both approaches before you decide which is the better choice for your business. Premium WordPress Hosting Pros Management – WordPress experts automatically update, back up, and fine-tune your site for you. That means you can spend less time dealing with back-end issues and more

Read More


Are Premium WordPress Themes Worth the Extra Cost?

If you are debating between using premium WordPress themes and one of the thousands of themes available for free, you’re faced with a difficult decision. Both types have pros and cons; use this quick guide to help you make the smart choice for your business. Free WordPress Themes Pros Cost – These themes are free, making them a smart choice if you have a limited budget, or if you only plan to make very limited use of WordPress. Selection –

Read More


4 Signs You Chose the Wrong Managed WordPress Hosting Partner

The unfortunate truth is that not all managed WordPress hosting providers are created equal, and some can even have a detrimental effect on your site. If you have recently signed up with a new hosting partner and you are trying to evaluate their performance, these 4 red flags could indicate you’ve made the wrong choice. 1. Your Site Slows Down Speed is an integral component of the success of your site. Great managed WordPress hosting providers use tools like managed

Read More


Pressable and the “Poodle” SSL Vulnerability.

We are aware of the recently announced vulnerability in SSL called POODLE.  This vulnerability affects a very small percentage of traffic on our systems. You can read more about this vulnerability here: Official Announcement at SANS https://www.openssl.org/~bodo/ssl-poodle.pdf The “fix” for this issue is to disable SSLv3 on servers and/or clients that utilize it. Fortunately, it is in use in a very small percentage of traffic and is not likely to hinder traffic to sites we host in any significant capacity.

Read More