Six useful things you can do with WP-CLI

Last updated on 2020/08/12

|

by Janez Troha

Featured post image

At WooCart we strive to make complicated things simple for the average WooCommerce user. One of those is using WP-CLI. At a regular host, you would have to first install and then figure out how to connect to the server to start using WP-CLI. On WooCart we make it super simple and you can run it easily in your browser in our dashboard.

Once connected you are presented with the below screen. You might be wondering what you can use WP-CLI for, so here’s a guide for a couple of nifty things.

1. Disable the plugin that is causing your page not to load

When an error happens on a store, we capture it and analyze it. You can see the culprits in the report. Often times, the WordPress dashboard is not accessible and disabling of plugins through it is not available.

In that case, we can use WP-CLI. To disable it you run:

wp --skip-plugins plugin deactivate redis-cache

We used `–skip-plugins` to prevent faulty plugin interfering with deactivation.

2. Import all external images in posts into WordPress media library

Let’s say that you created products or posts with an external tool that is still linking images to external websites. This commonly happens if you’re dropshipping and your images are still linked to the source website.

In this case, use `wp media sideload` that comes bundled in WooCart or you can install it from our public repository. It allows you to download an image, save it into the media library, and then use that image in your post. For example, let’s say that we want to import all images that are hosted on AliExpress servers for all products.

wp media sideload cdn.aliexpress.com --post_type=product

It’s that simple.

3. Replace all instances of old email in posts and settings in one go

Let say that you decided to change the support email address and that you don’t know where that email address is actually configured, or you might have also mentioned it in product descriptions and configured plugins with it. Now, product descriptions might be an easy task, but plugins are usually storing settings in a serialized format which is not easy to edit.

Fear no more: `wp search-replace` comes to the rescue.

wp search-replace oldemail@domain.com new@domain.com

This simple command will replace all old emails with the new one.

4. Make a quick backup of a database

Running search-replace might sound scary so you can always create a quick backup if something goes wrong.

wp db export 

and if you want to restore a backup

wp db import backup.sql

5. Clean up posts meta

When we transfer a store to WooCart we also do some optimizations and cleanup. One of the things that we look at is leftover data from various image optimization plugins.

For example, each plugin will create a meta file that contains data about image optimized. And if the plugin is no longer installed the data will actually slow down database queries. Because WP-CLI allows you to run arbitrary database queries we can first run command to see what kind of metadata is stored and how much storage it is using.

wp db query 'SELECT meta_key, ( SELECT COUNT(*) FROM wp_postmeta WHERE meta_key = m.meta_key ) AS count, (SELECT sum(char_length(meta_value)) / 1024 FROM wp_postmeta WHERE meta_key = m.meta_key ) AS size_kb FROM wp_postmeta as m GROUP BY meta_key ORDER BY `size_kb` DESC;' 

Which returns table looking something like this

+-----------------------------------------------+-------+----------+
| meta_key                                      | count | size_kb  |
+-----------------------------------------------+-------+----------+
| _wp-smush                                     | 23265 | 61255.20 |
| _customer_user_agent                          |   744 | 138.5469 |
| _billing_address_index                        |   745 |  84.6035 |
| _wc_attachment_source                         |   219 |  35.3438 |
| _wp_attached_file                             |   265 |  27.6104 |
| _cart_hash                                    |   744 |  23.2500 |
| _billing_address_1                            |   744 |  20.4170 |
| _wp_suggested_privacy_policy_content          |     4 |  16.7734 |
| _billing_email                                |   744 |  16.3828 |
| _order_key                                    |   745 |  16.0059 |
| _wpimage_size                                 |   264 |  14.4365 |

We can see that `_wp-smush` is using 60MB of the database. We can remove metadata from `_wpmush` by running.

wp db query 'DELETE FROM `wp_postmeta` WHERE `meta_key` LIKE '_wp-smush';'

This might sound a bit daunting, but if you transfer your store to WooCart, we’ll do that for you, for free.

6. See what is slowing down your site

You can install custom packages to WP-CLI that extend its functionality even further. One of those handy ones is also profile-command, which enables you to see which plugins are making requests slow.

After a recent update of woocommerce-admin, we noticed that the page is slower compared to previous versions.

wp profile hook init --spotlight --fields=callback,time --url=https://rosesntech-d0a.eu.mywoocart.com 

Running we soon spotted the culprit to be in the Automattic\WooCommerce\Admin\Install::check_version callback, which seems to be running on every request and adds quite a lot to the response time. We submitted the bug to upstream to verify if this is really as it was intended to be.

+--------------------------------------------------------------------+---------+
| callback                                                           | time    |
+--------------------------------------------------------------------+---------+
| WooCommerce->init()                                                | 0.0053s |
| wp_widgets_init()                                                  | 0.0108s |
| YITH_Woocompare_Frontend->init_variables()                         | 0.0003s |
| Automattic\WooCommerce\Admin\Loader::load_features()               | 0.0022s |
| WC_Post_Types::register_post_types()                               | 0.0033s |
| Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry->init | 0.001s  |
| ialize()                                                           |         |
| Automattic\WooCommerce\Admin\Install::check_version()              | 0.3946s |
| WooCommerce->add_image_sizes()                                     | 0.0004s |
| wc_yotpo_redirect()                                                | 0.0002s |
| Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes_Home_Screen_Feed | 0.0006s |
| back->on_init()                                                    |         |
+--------------------------------------------------------------------+---------+
| total (10)                                                         | 0.4185s |
+--------------------------------------------------------------------+---------+

Is there more?

As you can see WP-CLI is a very versatile tool and we barely scratched the surface of what we can do. You can check what else is possible on the official documentation.

If you have questions or know other tricks let us know 🙂