wpcli-power-tips

WP-CLI Power Tips You Absolutely Need to Know!

The WordPress command-line interface, or WP CLI for short, has always been popular among developers because it provides much more performance and speed when managing a WordPress site. At first glance, it may seem hard to believe that using the command line to perform certain tasks can in fact be easier than using the GUI. However, as you get used to the CLI, you’d realize it can boost your workflow and save a good deal of time. 

WordPress provides a graphical user interface for every administrative task, and this has helped make it the most popular content management system on the web. But in terms of performance, working with the command line allows you to perform many such tasks more efficiently and quickly.

In this article, we will be taking a look at some WP-CLI power tips.

WP-CLI Power Tips You Absolutely Need to Know!

WP-CLI is a tool that allows you to install WordPress, make backups, import and export a database, work with plugins, make setting changes, and work with records and taxonomies using the command line.

WordPress Update

It is one thing to install WordPress, another to update it. And just like installing WordPress, updating is also very easy with the WP-CLI. We will update WordPress using subcommands. The WP-CLI subcommands allow users to update system files and database files if they are not updated. Here’s how to do it:

wp core update wp core update-db

Updating WordPress with a dashboard may seem much more efficient than using the command line, but if you have multiple WordPress sites, using WP-CLI will save you time. Using the WP-CLI, you can easily update multiple WordPress sites with this command:

#! / bin / bash announce -a sites = ('/ var / www / wp1' '/ var / www / wp2' '/ var / www / wp3') for website in "$ {sites [@]}"; do wp --path = $ site core update done

Before any upgrade process, it is always advisable to back up your content, especially the databases.

Database Backup

WP-CLI has a one-line WordPress database backup command:

export database backup.sql

It’s so easy! By running this command, you have backed up the database files of your WordPress blog.

Installing Themes

Installing themes from the WP-CLI is very easy and takes less time as compared to when doing it using the WordPress dashboard. WP-CLI connects your server directly to the WordPress theme directories and allows you to download them in just a few seconds. You can install the theme using this command:

wp theme install themename

Now, the next part: how to activate the theme?

After installing the theme, you can activate it via WP-CLI using the following command:

wp theme activate themename

Activating themes with WP-CLI is very simple, and you’ll see that the changes will take effect in a few seconds.

And lastly, how to update the theme?

As you probably must’ve guessed by now, updating WordPress themes with WP-CLI is as easy as installing and activating it. The update command will save you a lot of time (use the first one given below to update a specific theme; or the second one to update all themes, perchance they have an update available):

wp theme update themename
wp theme update –all

Working with Data

In addition to simple post create commands like post edit and post delete, WP-CLI provides tools for managing posts. If you need a lot of posts to test code in a plugin or theme, you can use the post generate command:

wp post generate --count=1000

You can also export the current content and transfer it to another WordPress installation. To do this, you need to install the Importer plugin:

wp plugin install wordpress-importer --activate

Search and Replace

Developing a website on a local server or development server, and then moving to another server when everything is ready, is a common practice. Copying files and migrating the database are simple steps. Replacing old URLs with new ones in database records is a more complex part of this procedure, since URL addresses also exist in serial data, for which a simple search and replace will not work.

The WP-CLI search-replace command will help you complete this task. The search-replace command replaces the old URL with the new one in all database records, including those that contain serialized data.

wp search-replace 'something.example.com' 'www.example.com'

This search and replace command helps developers to complete the creation of the website in less time than expected. Most of the work is done by the WP-CLI, so you don’t have to worry about small errors or forgetting to update the URL for a particular page. In this case, the WP-CLI unpacks the JSON data, performs the replace action, and packs the data back into the database input.

If you want to see how many instances of this search action are in your database without running the replace command, you can run the previous command with-dry-run using:

wp search-replace --dry-run 'something.example.com' 'www.example.com'

Installing Plugins

Installing plugins using the WP-CLI is a simple task. WP-CLI installs all available plugins in the WordPress plugin directory. You can use this command to install plugins:

wp plugin install pluginname

Just replace “pluginname” with the name of the plugin you want to install on your site.

Next step, let’s activate the plugin! To activate the plugin, use the following command:

wp plugin activate pluginname

And when done, time to deactivate the plugin! If you want to disable the plugin, just run the following command:

wp plugin deactivate pluginname

Media

Some WP administrative tasks that are difficult to perform for beginners relate to images and media in general. WP-CLI provides some tools to save time.

Bulk Import Images

It is not uncommon for a client to provide a set of images and ask you to import them to their site. Doing this through the admin panel is too time-consuming. Instead, using the WP-CLI media tool, you can complete this task in a single command:

wp media import path/to/images_folder/*

Regenerating Media

When creating new image sizes during development, you need to regenerate the thumbnails. Of course, you can use a third-party plugin or custom PHP code to achieve this goal, but it takes significantly more time to achieve this goal than the WP-CLI command given below (not to mention that installing a separate plugin will consume more memory in the longer run):

wp media regenerate

There are endless possibilities here because you can combine multiple commands to specify the target images to be captured. For example, to regenerate images for posts of a certain category, you can use the following command:

wp media regenerate $(wp eval'foreach( get_posts(array("category" => 4,"fields" => "ids")) as $id ) { echo get_post_thumbnail_id($id)." "; }')

Conclusion

WP-CLI is really powerful tool that can be used to manage WordPress sites from the command line. In addition to the commands mentioned above, there are many other commands for managing your database, creating backups, managing posts, comments and WordPress Multisite that can certainly save your time and make site management really easy.

That said, by now you should have a good idea of the capabilities of the WP-CLI. These possibilities become endless as you get the hang of WP-CLI and use it more and more. See our first post on the subject in our Beginner’s Guide to WP-CLI.


Our Guiding Principles

  • Provide consistent, stable and reliable web hosting services.
  • Ensure rapid ticket response and quick resolutions to issues.
  • Never saturate or over-provision servers to ensure stability and speed for our customers.
  • Use only high quality enterprise-class hardware to ensure minimal downtime from hardware failures.
  • Provide clear pricing with no hidden fees or gotchas.