SOLUTION: Run WP-CLI without clicking Open Site Shell

Someone else asked:

How do I run wp-cli without clicking Open Site Shell?

Below is how I fixed that on my Mac!

  1. Currently, I’m running the latest version of Local, macOS Sonoma 14.7.2, and use the Zsh shell. My shell is customized with many other things, so my screenshot below may look different from your shell, but it should work on a computer with similar specs.

  2. If you are using the Zsh shell on a Mac and have Local (localwp) already installed, create the alias below (info on how to create custom Oh My Zsh aliase or another way to make an alias on your Mac):

    ## Run Localwp shell that kicks off WP-CLI & 
    ## access to mysql shell
    alias localshell='sh "$(ls -t ~/Library/Application\ Support/Local/ssh-entry/*.sh | head -n 1)"'
    
  3. After saving your aliases file, in your terminal go to your home (run cd) and then refresh your aliases file (source ~/.zshrc).

  4. In your terminal, go the root location of a WordPress site you created with Local (like /Users/myusername/Desktop/projects/wpsite/app/public).

  5. Finally, run localshell and you should see output like in the screenshot below:

  6. If you see output like the screenshot above, you should be able to run WP-CLI commands like wp plugin list (view all plugins installed) or mysql (to enter the MySQL shell and run SQL commands).

@risingPhoenix1979

alias localshell='sh "$(ls -t ~/Library/Application\ Support/Local/ssh-entry/*.sh | head -n 1)"'

Why would you pick a random (last modified) shell script file in the ssh-entry folder instead of the actual shell script file for the site in question with the correct paths? The shell script will change the working directory to the location of the app/public folder for the specified site in the shell script. I’m sorry but I can’t really see how this will work.

cd "/Users/<user>/Local Sites/<site>/app/public"

The other thing you can do, which I do just so I have the WP-CLI anytime for its use to remotely manage live sites, is install it globally on your Mac. This can be done with brew or composer. Then, at least for WP-CLI, the wp command is accessible in any shell without ever running the shell scripts Local generates.

1 Like

Instead of messing around with multiple instances and different versions you can make the php and wp-cli commands included by the Local app available everywhere by adding them to the shell $PATH variable. And then you can use the which command to check that the symlinks are added to the /usr/local/bin directory and the php and wp-cli commands now are available everywhere via the terminal. You might also want to check out the WP-CLI global parameters documentation and the use of YAML configuration files.

wp macOS Intel and Silicon

sudo ln -sf /Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar /usr/local/bin/wp

php macOS Intel

sudo ln -sf /Applications/Local.app/Contents/Resources/extraResources/lightning-services/php-8.2.23+0/bin/darwin/bin/php /usr/local/bin/php

php macOS Silicon

sudo ln -sf /Applications/Local.app/Contents/Resources/extraResources/lightning-services/php-8.2.23+0/bin/darwin-arm64/bin/php /usr/local/bin/php
$ which wp
/usr/local/bin/wp

$ wp cli version
WP-CLI 2.11.0

$ wp cli version | awk '{print $2}'
2.11.0
$ which php
/usr/local/bin/php

$ php --version | head -n 1
PHP 8.2.23 (cli) (built: Sep  2 2024 15:30:45) (NTS)

$ php -v | awk '{print $2; exit}'
8.2.23
2 Likes