Specify a custom $PATH to WP-CLI when using wp –ssh

If the wp binary isn't already on your $PATH, you'll need to define it on the fly.

wp --ssh=<host> is a new feature in WP-CLI v0.24.0 where you can perform WP-CLI commands on remote servers over SSH. However, the feature requires having wp accessible on the $PATH on the remote server. Because SSH connections don’t load .bashrc or .zshrc, you may need to specify a custom $PATH when using wp --ssh=<host>.

You can do so by hooking into the before_ssh hook, and defining an environment variable with the command you’d like to run:

WP_CLI::add_hook( 'before_ssh', function() {

    $host = WP_CLI\Utils\parse_ssh_url( WP_CLI::get_runner()->config['ssh'], PHP_URL_HOST );
    switch( $host ) {
        case 'runcommand.io':
            putenv( 'WP_CLI_SSH_PRE_CMD=export PATH=$HOME/bin:$PATH' );
            break;
    }
});

If you put the code above in a pre-ssh.php file, you can load it for your entire environment by requiring it from your ~/.wp-cli/config.yml file:

require:
  - pre-ssh.php