Run wp-cli of a local site via non-interactive script

I like to automate some test routines and need to provision several local sites automatically.

I know that I can manually right-click and open a site shell to run wp-cli commands for that site. However, I’m looking for a way to run a script WITHOUT clicking anywhere.

The current LocalWP app (Lightning) doesn’t use docker anymore, so the previous answers on this forum are not working.

When I’m in the default shell (not in a local site shell), can I somehow pipe commands to a site shell and then close it again?
Or is there a way to SSH into a running machine in Lightning?

I found a non-interactive solution to run bash commands for a certain local site :slight_smile:

Here’s the bash script for that - just make sure to start the local site before running it:

#!/bin/bash

# Runs some WP CLI commands inside a local site.
run_in_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!)
	SHELL="" 

	# Set up the LocalWP environment.
	source "~/Library/Application\ Support/Local/ssh-entry/$1" \
		&>/dev/null

	# 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)...

$(run_in_local_site O3hzP9siT.sh)
$(run_in_local_site Bi2Hj9eod.sh)

Instead of using a custom function (run_in_local_site) you could also shorten the code to a 1-liner, like this:

#!/bin/bash
echo $(SHELL= && source ~/Library/Application\ Support/Local/ssh-entry/O3hzP9siT.sh &>/dev/null && wp option get siteurl)
1 Like

This is cool, thanks for sharing!

In a similar vein, for sites that are longer-lived and which I want to quickly be able to jump into their shell, I’ll manually call that script in a default shell and append a bash comment of whatever makes sense for me to remember the site.

From there, I can use ctrl+r to do a reverse search for that comment and quickly jump to that Local site’s environment:

1 Like

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.