Created
July 28, 2021 22:16
-
-
Save koko-ng/29277fe712bb0c587b664d6a56e6f375 to your computer and use it in GitHub Desktop.
Automatically install wp-cli on first connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Automatically install wp-cli on remote hosts if absent | |
* | |
* @author koko-ng <[email protected]> | |
* @package koko-ng/wp-cli-config | |
*/ | |
WP_CLI::add_hook( | |
'before_ssh', | |
function () { | |
$host = WP_CLI\Utils\parse_ssh_url( | |
WP_CLI::get_runner()->config['ssh'], | |
PHP_URL_HOST | |
); | |
$scheme = WP_CLI\Utils\parse_ssh_url( | |
WP_CLI::get_runner()->config['ssh'], | |
PHP_URL_SCHEME | |
); | |
// Default home in docker images is /var/www. | |
if ( 'docker' === $scheme ) { | |
$home = '/tmp'; | |
} else { | |
$home = '$HOME'; | |
} | |
putenv( | |
sprintf( | |
'WP_CLI_SSH_PRE_CMD=[ ! -f "%1$s/.bin/wp" ] && (mkdir -p %1$s/.bin; cd %1$s/.bin/; curl -o wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar; chmod +x %1$s/.bin/wp); export PATH=%1$s/.bin/:$PATH', | |
$home | |
) | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Automatically install wp-cli on remote host
wp-cli is installed and added to path if it isn't available.
Installation
Save the above file in
~/.wp-cli/
In
~/.wp-cli/config.yml
put