How to use WP-CLI without SSH access to the server

This micro-tutorial is now continuously revised on runcommand.

Need to use WP-CLI on a WordPress site, but don’t have SSH access to its server? Simply change your local WordPress install’s database credentials to use the remote site’s configuration, such that the local WordPress install is using the remote database. You’re now free to run commands to your heart’s content.

Keep in mind:

  • The remote WordPress site’s database will need to accept public connections, or you’ll need to be able to whitelist your IP address.
  • Your local WordPress install should use the exact same codebase as the remote install.
  • Any filesystem operations will be happening on your local machine, not the remote server. If you install and activate a plugin, the remote WordPress site will then try to run a plugin that doesn’t exist on its server.
  • Bad things can happen. Be very careful.

But, I used this trick for data transformation on a Flywheel-hosted site earlier this week, and it worked like a charm.

3 Comments

Weston Ruter May 18, 2016 Reply

I use this approach with running WP-CLI from my host machine but connecting WP inside of Vagrant. In a wp-config.php I have something like this:

if ( file_exists( '/vagrant' ) ) {
	define( 'DB_HOST', 'localhost' );
} else {
	define( 'DB_HOST', '192.168.50.4' );
}

Where the second DB_HOST is the IP for the guest VM. See also a VVV PR which does this with an aim for allowing PHPUnit to be run from the host machine (which I also blogged about). This is also part of our forked Pantheon upstream.

Another caveat for running WP-CLI remotely like this is the persistent object cache wouldn’t get updated when changes are made, unless you also update your wp-config.php to connect to the Redis/Memcached server running inside Vagrant if you’re running from the outside.

Jonathan Mayhak (@Jmayhak) May 19, 2016 Reply

Did you have to work with Flywheel to whitelist your IP? I thought Flywheel forced you to work w/ your site’s DB through their web UI. I considered that a pretty big negative against them.

Daniel Bachhuber May 19, 2016 Reply

I had to whitelist my IP through their dashboard, which I didn’t find to be much of a hassle.

Leave a Reply