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.

Comments

  1. 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.

Hand Built is Daniel Bachhuber, an effective developer with technology leadership you can trust.

How can I help?

Hit me at [email protected] with a short note about what you'd like to do.

My roots are WordPress and publishing, and I love pretty much everything related to web development (architecture, performance, security, etc.). When not working with clients, I also maintain WP-CLI (WordPress' command line interface) and other open source projects.