WordPress Management with WP-CLI

0
1418
5 min read

Managing WordPress via the web interface can be a struggle sometimes. Even Common tasks can take a lot of effort to complete, especially when they are recurring.

Luckily, with the WP-CLI (WordPress Command-Line Interface), you have a command line tool to manage your WordPress installations.

In this post, I will show you how to install WP-CLI, get a WordPressinstallation up and running, and manage WordPress themes and plugins.

Install WP-CLI

When it comes to installing WP-CLI, all you have to do is check the system requirements, download the WP-CLI archive, and make it executable.


Checking system requirements

To use a command-line tool like WP-CLI, you need shell access to your web server’s filesystem.

This can be achieved through your terminal application on your local machine or via ssh on a remote host. Most serious hosting plans, and every server plan, will provide you with at least one SSH account.

The other requirements are very basic:

  • UNIX-like environment (OS X, Linux, FreeBSD, Cygwin)
  • PHP 5.3.29 or later
  • WordPress 3.7 or later

Downloading the WP-CLI PHP archive

WP-CLI is distributed in a PHP Archive file (.phar). So at your shell’s prompt, download the wp-cli.phar using wget or curl:

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

To verify the download, execute the archive with the php binary:

$ phpwp-cli.phar --info

If the download issuccessful, you will see an output like this:

PHP binary: /usr/bin/php PHP version:    5.5.29 php.ini used:    WP-CLI root dir:    phar://wp-cli.phar WP-CLI packages dir:     WP-CLI global config:    WP-CLI project config:   WP-CLI version: 0.24.0 

Now you are ready to use WP-CLI to manage your WordPress installations, but to save time and typed characters, it’s a commonly used convention to call WP-CLI with the wp command.

Set up the wp command for execution

To avoid typing phpwp-cli.phar … every time you want to use WP-CLI, you have to modify the archive’s file permissions and move it to somewhere in your PATH or add an alias to your shell.

To make the archive executable, edit its permissions with chmod:

$ chmod +x wp-cli.phar

After this,move the archive to a directory somewhere within your PATH:

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

If you are on shared hosting, you might have not sufficient rights to move an executable to /usr/local/bin/. In that case, you can add a shell alias to your shells configuration.

Put the following line in the .bashrc or .profile file in your home directory, depending on your shell and your operating system:

aliaswp='~/wp-cli.phar'

To make the shell recognize the new alias, you have to reinitialize the configuration file. For example:

$ source .bashrc

Now you can use the wp command to manage WordPress with WP-CLI.

Let’s start with some basic tasks.

Managing WordPress with WP-CLI

With WP-CLI, you can manage each and every aspect of your WordPress installation that can be managed through the WordPress admin area—and quite a few more.

To get a glimpse of the tasks that can be done with WP-CLI, you simply type:

$ wp help

But let’s start with installing the WordPress core.

Install a blog

To geta fresh WordPress installation done, you just need your database information and a few commands.

To download the latest WordPress core,simply type:

$ wp core download 

You can also specify the version and language you want to download:

$ wp core download --locale=de_DE --version=4.5.2 

To create the configuration file (wp-config.php) use the core config command:

$ wp core download --locale=de_DE --version=4.5.2 

To finalize the installation process, you have to configure your blog’s information and the admin user data:

$ wp core install --url="http://yourdomain.com" --title="My new blog" --admin_name="admin" --admin_email="[email protected]" --admin_password="secretpassword" 

You can now point your browser to your WordPress admin login and start blogging.

Managing WordPress themes and plugins

After installing WordPress WP-CLI, you can start using it to manage your themes and plugins.

Managing themes

Managing themes with WP-CLI is much quicker than using the WordPress admin area.

To install and activate a theme from wordpress.org, you type:

$ wp theme install twentysixteen --activate 

You can also install a theme from a local .zip file:

$ wp theme install ../mynewtheme.zip

Or from aURL:

$ wp theme install https://github.com/Automattic/_s/archive/master.zip

To list all installed themes,type:

$ wp theme list 

Managing plugins

Managing plugins with WP-CLI is nearly the same. You use the wp plugin command instead of wp theme.

When you don’t know the slug of a plugin, you can search the wordpress.org plugin repository:

$ wp plugin search "yoastseo" 

You will get an output like this:

Success: Showing 10 of 285 plugins.
+-------------------------------------------+-------------------------------------+--------+
| name                                      | slug                                | rating |
+-------------------------------------------+-------------------------------------+--------+
| Yoast SEO                                 | wordpress-seo                       | 80     |
| Meta Box for Yoast SEO                    | meta-box-yoast-seo                  | 0      |
| Remove Yoast SEO comments                 | remove-yoast-seo-comments           | 0      |
| ACF-Content Analysis for Yoast SEO        | acf-content-analysis-for-yoast-seo  | 100    |
| qTranslate-X& Yoast SEO              | dennisridder-qtx-seo                | 0      |
| SEO Advanced Custom Fields Analyzer       | seo-advanced-custom-fields-analyzer | 0      |
| Integration: Yoast SEO & qTranslate-X | wp-seo-qtranslate-x                 | 72     |
| Uninstall Yoast SEO                       | uninstall-yoast-seo                 | 100    |
| Remove Branding for Yoast SEO             | remove-branding-for-yoast-seo       | 84     |
| All Meta Stats Yoast SEO Addon            | all-meta-stats-yoast-seo-addon      | 100    |
+-------------------------------------------+-------------------------------------+--------+

Find the slug and install it afterward:

$ wp plugin install wordpress-seo

Then you can activate the plugin with:

$ wp plugin activate wordpress-seo

Deactivating a plugin is pretty much the same:

$ wp plugin deactivate wordpress-seo

Conclusion

You should now have a basic understanding of how to manage WordPress sites with WP-CLI.You can also use WP-CLI to manage your database, do backups, keep things up to date, handle posts and comments, and maintain WordPress multi-sites.

Its full strength is especially exploited when used in shell scripts, or in a more development-style app environment with version control, unit testing, and multi-stage deployment.

About the author

Marcel Reschke is a developer based out of Germany. He can be found on GitHub .

LEAVE A REPLY

Please enter your comment!
Please enter your name here