If you closely examine CaptainCore, beyond the golang web server and Vue.js WordPress plugin, you’ll see it’s really just WP-CLI commands and bash scripts. WordPress’ WP-CLI project is one of the most powerful and important WordPress projects. Anything with WP-CLI can be scripted and automated. That’s invaluable for WordPress host and maintenance providers and preciously why I’m building CaptainCore on top of it.
Hacktoberfest is a great excuse to take time and contribute back to WP-CLI.
For me, this is long overdue. It seems like contributing to an open source project always gets moved to the non-important part of my TO DO list. This year’s Hacktoberfest is over and I’m happy to report that I did manage to close some outstanding Github issues and had a few pull requests merged into WP-CLI. One those was a new global parameter
--exec which is something new that I proposed.
--exec allows you to run arbitrary PHP before the command runs.
The is the same as the
--require global parameter except the code doesn’t need to be put in a separate PHP file. So really this new parameter adds nothing new in terms of functionality. It’s just an easier way of handling PHP one liners. Let’s dig into a real world example.
A little background into WP-CLI theme and plugin updates.
Running theme and plugin updates with WP-CLI is very common use case. However triggering the updates from WP-CLI doesn’t 100% mimic going to the
/wp-admin/update-core.php and running the updates from there. There is a long standing debate within WP-CLI whether or not it should fully mimic, see here: https://github.com/wp-cli/wp-cli/pull/674. The workaround fix is to create a separate PHP file containing
define( 'WP_ADMIN', true ); and then require that file while doing the updates. Sound simple right? Well it get’s messy when your trying to automate. Within a bash script I commonly do something like this.
# Generate require-wp-admin.php file echo "<?php define( 'WP_ADMIN', true ); ?>" > require-wp-admin.php # Run plugin updates wp plugin update --all --require=require-wp-admin.php
All of that seems overkill for something as simple as define a constant WP_ADMIN to true. However there isn’t an easy way to handle… until now. That’s preciosuly what
--exec does. It allows you to run arbitary PHP code before the command is run all within a single command. The above code gets nicely re-written to the following:
wp plugin update --all --exec="define( 'WP_ADMIN', true );"
This is currently just a preview of what’s going into WP-CLI. In order to be able to use
exec you’ll need to wait until the next major release v2.5.0. Or you upgrade WP-CLI to the latest nightly build and try it out right away.
wp cli update --nightly