WP-CLI is the command-line tool for interacting and managing a WordPress site. I’ve used the limited version we have for supporting some sites on WordPress.com before, but my self-hosted sites are hosted at Pressable which (at least, last I checked), doesn’t support WP-CLI, so I wouldn’t say I’m comfortable with it.
I’m starting on a new project (a single-page application built on WordPress), and my coding mentor has instructed me to use wp scaffold plugin for it.
I mostly use Local by Flywheel for my local sites (though wp-env via Docker is also very useful). Local sites come with WP-CLI already installed, but you need to open a site Shell from within Local to use it.
In my search for that information, I also came across several articles explaining how to get around that requirement, to just run WP-CLI directly from Terminal, so I thought I’d give that a try.
You need to add two files to the Local site’s root folder to tell WordPress where to find WP-CLI. For a future reference to myself:
wp-cli.local.yml must contain this content:
path: app/public
require:
- wp-cli.local.php
And wp-cli.local.php must look like this, with the localhost value being the “Socket” value under the Database tab for the site in Local:
<?php
define('DB_HOST', 'localhost:/Users/sal/Library/Application Support/Local/run/lBW9-Gu_J/mysql/mysqld.sock');
// Only display fatal run-time errors.
// See http://php.net/manual/en/errorfunc.constants.php.
error_reporting(E_ERROR);
// Disable WordPress debug mode.
// See https://codex.wordpress.org/WP_DEBUG.
define('WP_DEBUG', false);
Props to Sal Ferrarello for the detailed explanation.
Of course, after doing this WP-CLI didn’t work, cause apparently this doesn’t use the WP-CLI version already installed on the Local site, but instead looks for it on my computer itself. So as the final step I installed WP-CLI using Homebrew.
And now I can manage my local site using WP-CLI. Yay!
Leave a comment