Scaling WordPress: Using wp-cli for WebOps
Tags: wordpress, webdev, linux, bash
Published: 2020-01-09 17:24:58

Maintaining a high traffic WordPress or WooCommerce site takes a lot of work. While automation takes care of tasks such as log rotation, daily backups, and monitoring, ongoing performance optimization still requires manual attention.

When you are working on a mission-critical WordPress site, you can’t install plugins for operational tasks without risking outages or performance drop. As you know, plugins can be a liability on high-traffic sites.

This is where wp-cli comes in handy. Wp-cli is the command line interface for WordPress. It uses the REST API and provides functionality that would otherwise require plugins or code snippets.

Wp-cli is quite useful in high-availability environments because you can use it with PHP-CLI without blocking any php workers. Another upside to using wp-cli instead of plugins is that php-cli has an unlimited execution time with no timeouts by default. This means that if you need to run a query on a large dataset, wp-cli can handle it without timing out halfway.

Here are a few examples where WebOps teams can utilize wp-cli:

**Manage wp-cron:**
Wp-cli provides better control over cron events. This means you can run specific cron events as needed instead of merely calling wp-cron.php and letting it timeout while clearing expired transients.

This command can execute all pending events without timing out after 60 seconds:
`wp cron event run –due-now`

**Delete expired transients:**
While expired transients should delete automatically and keep the wp_options table lean, sometimes it is essential to delete them more frequently. Poorly coded plugins generate transients that stick around for longer than necessary, and the following command can get rid of them:

`wp transient delete –expired`

If you need to delete all transients, this command helps:

`wp transient delete –all`

**Regenerate Thumbnails:**
Instead of adding a plugin to regenerate thumbnails (And slowing down wp-admin), following command can do it for you more effectively:

`wp media regenerate`

To selectively regenerate thumbnails you can pass the attachment id or instruct wp-cli to regenerate all missing thumbnails:

`wp media regenerate <attachment-id> --only-missing`

**Database operations:**
Wp-cli uses the db credentials specific in wp-config.php and provides an easy interface for routine database operations. This comes in handy when you are using RDS, Aurora or other remote MySQL solution where the hostname is complex, and you would rather not keep typing it for each operation.

DB Export:
`wp db export <filename>`

DB Import:
`wp db import <filename>`

DB Reset:
`wp db reset`

DB Repair:
`wp db repair`

DB Optimization:
`wp db optimize`

**User Management:**
WebOps teams need to create users with different roles from time to time to test wp-admin functionality. Wp-cli makes it easy, secure, and fast:

`wp user create demouser wp@demo.com --role=editor`

Once you are done with the user account, you can delete it with this:

`wp user delete demouser`

**Cache Management:**
Wp-cli can manipulate the object-cache without requiring any separate plugins.

To quickly purge the object-cache, this command helps:

`wp cache flush`

**Core and Plugin updates:**
Wp-cli can update the core and plugins from the command line:

`wp core update`
`wp plugin update --all`
`wp plugin update wordpress-seo`

The ability to run non-interactively, single .phar binary, and extensibility using modules makes wp-cli a great tool for deployment and ops automation.