If you already don’t know about WP-CLI, its a pretty great tool to manage a WordPress install via terminal commands. This essentially gives you a lot of power enabling you to interact with the WordPress install just by commands and not having to interact with UI at all. Read more at [WP-CLI’s homepage](http://wp-cli.org/).
It can also be extended to add custom commands. The usual way to do can be read in detail here – [https://github.com/wp-cli/wp-cli/wiki/Commands-Cookbook](https://github.com/wp-cli/wp-cli/wiki/Commands-Cookbook) but the essence is to create a new PHP Class extending from `WP_CLI_Command` and then just add that class as a handler for a command by calling `WP_CLI::add_command()`.
Now for instance, what if you want to redefine an existing wp-cli command? The way to do that is to create a new PHP Class as if you were creating a new custom command, and then instead of extending it from `WP_CLI_Command`, extend it from the PHP Class which defines the command you want to override. Simple enough! Here’s an example:
`wp transient delete-all` lets you delete all transients from the database. Yes, database. If you know about persistent caching storage options with WordPress like Memcache or APC, then transients are not actually stored in database anymore. They are simply stored in the persistent cache storage. And since the delete-all subcommand of `transient` command only runs a SQL query to delete them from database, it simply does nothing if the site uses persistent cache storage (Object cache drop-in `object-cache.php`, if that reminds you of it).
So you can define a new PHP Class something like this:
<?php | |
if ( defined(‘WP_CLI’) && WP_CLI ) { | |
class Transient_Mod_Command extends Transient_Command { | |
public function delete_all() { | |
// new definition of delete-all subcommand | |
} | |
} | |
WP_CLI::add_command( ‘transient’, ‘Transient_Mod_Command’ ); | |
} |
Pretty straight-forward, right?
Using the same technique, you can add new sub commands to an existing command as well by simply defining a new method.
On the transient issue note, why that shouldn’t be fixed in WP-CLI itself is a question that intrigues me as well. From a quick look at their Github issues, seems like they want to support it in a way it works correctly with persistent cache storage, but haven’t got to that place yet. I will see how soon I can create a patch for it and send a pull request.
Have fun with WP-CLI!
Leave a Reply