I found a non-interactive solution to run bash commands for a certain local site
Here’s the bash script for that - just make sure to start the local site before running it:
# Runs some WP CLI commands inside a local site.
# The $SHELL variable needs to be empty; otherwise the local
# ssh-entry script will start a new, interactive shell
# process (not a sub-shell!)
# Set up the LocalWP environment.
source "~/Library/Application\ Support/Local/ssh-entry/$1" \
# Finally, we have a "ssh" environment that allows us to
# directly interact with the local site. We can run our
# commands here:
wp option get siteurl
The above function loads the LocalWP environment for a specified site and then runs one wp-cli command (just an example) - the site is specified by the
$1 variable, which is the first parameter of the function:
# Important: Run the command in a sub-shell!
# Otherwise the LocalWP settings will spill over to the current
# shell process (and change $SHELL, etc)...
Instead of using a custom function (
run_in_local_site) you could also shorten the code to a 1-liner, like this:
echo $(SHELL= && source ~/Library/Application\ Support/Local/ssh-entry/O3hzP9siT.sh &>/dev/null && wp option get siteurl)