WP-CLI + Laravel Valet = Cool Tricks

Here are 2 ways I use WP-CLI and Laravel Valet

I do not consider myself a command line ninja.  In fact, I prefer to write my scripts as php files and kick them off with #!/usr/bin/php (still need to make them executable with chmod +x filename.php).

I am deeply in love with WP-CLI and Laravel Valet.  Both are command line tools. That means they can be intimidating with a HUGE list of commands that are mostly forgettable. But, I’ve found these 2 in particular have daily payoff for a small memory investment.

Backing up

One of my favorites.  I have a simple .php file executed by cron that looks at a directory and creates an array of directories inside. Then I exec(“wp db export /backuplocation/$dirname.sql”); which creates a simple backup file of the database.  In the same script, on the next line I run an exec(‘tar -cf /backuplocation/$dirname.tar *’);  One could put that .tar file anywhere…maybe even scp it somewhere?
Super double bonus for running this on a machine with dropbox/google drive/icloud and slapping your backup files in your folder-syncy-folder.
Oh! I forgot the best part.  After /backuplocation I have the day of the week.  So /backuplocation/$day/$dirname.sql would give me 7 rotating backups.  Obviously if you want to retain more you can run 30 days rotation or whatnot.

Local Dev

Installing Laravel Valet isn’t super easy, but it’s totally worth it. (plus if you aren’t on the homebrew bandwagon it’s time…)
I have a folder called ~/websites/ and in it are a bunch of odd-ball WP projects I’m working on.  Because I ran ‘valet park’ on that dir every directory I add in there resolves to $dirname.dev.

I have a php script in there that I run with:
./valetlocal.php newsite
It does the following:

  • Creates a new folder in ~/websites with that name.
  • Runs wp core download
  • Runs wp core config with the mysql settings I need (because I ‘brew install mariadb’)
  • Runs wp core install
  • Runs wp plugin install $newsite –activate

The wp plugin part fails most of the time but on occasion I’m spinning up a site to mess around with a plugin.  For those times I create a site with the plugin slug name and 25 seconds later I have a WP install with that plugin ready to go.  I could do the same for themes if I were to mess with them with some regularity.