• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / WP-CLI LocalWP without “Open Site Shell”
WP CLI LocalWP Without Open Site Shell.

WP-CLI LocalWP without “Open Site Shell”

Last updated on September 24, 2021 by Sal Ferrarello

I’ve become a big fan of LocalWP (formerly Local by Flywheel) for local WordPress development (i.e. running websites on my Mac). One thing I do find frustrating is the need to use “Open Site Shell” when I want to run a WP-CLI command.

If you’re in a hurry to set this up, you can jump to the Quick Setup below.

Configure WP-CLI to work without “Open Site Shell”

The key to making this work is adding two files wp-cli.local.yml and wp-cli.local.php.

Where to Add These Files

Both of these files go in the project root directory. The project root directory for the website contains three folders (/app, /conf, and /logs) by default. Your two new files with be alongside these directories.

What Goes In These Files

wp-cli.local.yml

path: app/public
require:
  - wp-cli.local.php

wp-cli.local.php

The contents of wp-cli.local.php will vary for each Local site. The information you will need can be found in the LocalWP dashboard for your site under the heading DATABASE. We are looking for the Socket value.

Database tab for a site in the LocalWP app showing a socket value of /Users/sal/Library/Application Support/Local/run/lBW9-Gu_J/mysql/mysqld.sock

In my screenshot the Socket value is: /Users/sal/Library/Application Support/Local/run/lBW9-Gu_J/mysql/mysqld.sock

We combine this Socket value with local:, to define the DB_HOST value in the file wp-cli.local.php

<?php
define('DB_HOST', 'localhost:/Users/sal/Library/Application Support/Local/run/lBW9-Gu_J/mysql/mysqld.sock');

// Only display fatal run-time errors.
// See http://php.net/manual/en/errorfunc.constants.php.
error_reporting(E_ERROR);

// Disable WordPress debug mode.
// See https://codex.wordpress.org/WP_DEBUG.
define('WP_DEBUG', false);

Confirm WP-CLI Works

To confirm everything is setup correctly from the command line (on your Mac, without using “Open Site Shell”) run:

$ wp option get siteurl

and if everything is working properly, you’ll get back the URL for your website.

Troubleshooting

My most common error at this point is forgetting to have the website running in the LocalWP Dashboard. We can’t use WP-CLI with the site, if the site is not running.

If you get a response like “command not found: wp”, then you likely need to install WP-CLI.

How Does this Work?

WP-CLI checks for config files named wp-cli.local.yml or wp-cli.yml inside the current working directory (or upwards).
By adding our wp-cli.local.yml to the project root, any command run anywhere inside the project will use our wp-cli.local.yml configuration (WP-CLI config documentation).

wp-cli.local.yml

We define the path to the WordPress install in relation to wp-cli.local.yml and we require the file wp-cli.local.php be run before any of the WordPress files.

wp-cli.local.php

We override the DB_HOST database connection value defined in wp-config.php to include the database socket specific to our site. Since our database connection information is set before wp-config.php, our information is used. Since WordPress tries to set these constants after they are already set, PHP throws notices. To suppress these notices we set PHP to only display fatal run-time errors.

Other Methods to Setup

Quick Setup

Run this command from the the project root directory of the site.

$ curl -O https://raw.githubusercontent.com/salcode/wpcli-localwp-setup/main/wpcli-localwp-setup  && bash wpcli-localwp-setup && rm -rf ./wpcli-localwp-setup

Running the following at the command line will:

  • download a bash script to create the two files (wp-cli.local.yml and wp-cli.local.php)
  • run the bash script (you’ll be prompted for the Database socket value, which can be found on the Database tab of the site in LocalWP)
  • delete the bash script, leaving the two new files behind

The Quick Setup files come from my salcode/wpcli-localwp-setup project.

Thank You

Thanks to my employer WebDevStudios who provides weekly time when our programmers can gather and chat about technology and bounce around ideas. The updates to make this compatible with LocalWP (instead of the old Local by Flywheel) came out of one of those meetings. Particular thanks to my co-workers Aubrey Portwood, Richard Aber, Ashar Irfan, and Justin Foell for all their input and solutions to problems I couldn’t figure out.

In addition to providing invaluable time for programmers to chat and bounce around ideas, WebDevStudios also sets aside time for Five for the Future, which is dedicated time for us to give back to the open source community. Some of this time I used to put together this information.

This project was originally inspired by Morgan Estes’s work with his project BigWing/Local-WP-CLI.

Thanks to Justin Sternberg for sharing his similar quick setup in his Dot-Files.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Dev Tips, Programming, Solution Tagged With: ssh, WordPress, wp-cli

Reader Interactions

Comments

  1. Kevin says

    August 14, 2018 at 5:59 am

    Good Article! But there is a big problem:
    When I hit “wp option get siteurl” in console it works fine… but have you ever tried to hit ” wp db export ” ? It will refuse the connection: “mysqldump: Got error: 2013: Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0 when trying to connect”

    Do you have any solution for that?

    Reply
    • Sal Ferrarello says

      August 14, 2018 at 12:20 pm

      Hi Kevin,

      Glad you’re finding this useful. I’m not sure about the problem you’re seeing, if I run

      wp db export

      it runs successfully and I get a file like local-2018-08-14-715f4c0.sql in my local directory.

      If I were troubleshooting the problem you described, as a next step I’d use the Local by Flywheel “Open Site SSH” and then try wp db export from there.

      Sorry I don’t have an answer. Good luck and if you get things sorted, I’d love to hear about how you got it to work.

      Reply
  2. Torin says

    January 2, 2019 at 7:49 pm

    That solved all my issues. Thank you!

    Reply
  3. Clifton says

    April 25, 2020 at 4:10 pm

    Do you have an update for this article for Local 5.2.8.
    Thanks

    [Note: Clifton included a video clip that further explained that using the new Local Lightning, there is no remote host nor remote port under the database settings.]

    Reply
    • Sal Ferrarello says

      April 27, 2020 at 6:42 am

      Thanks for the comment Clifton. I’ve not yet upgraded to Local Lightning, so I’m not familiar with what settings are needed there. At this point, I don’t have a timeline for making the update. If anyone else reading this has any resources to share, please feel free.

      I’ve also added a note to the top of the article, explaining that these steps do not apply for Local Lightning.

      Reply
  4. andre says

    June 25, 2020 at 5:34 am

    Cool, but I ran the terminal command and clicked through the database host and database port settings without checking them. Now I get a message that I have to check the database settings.

    How can I do that?

    Thanks!

    Reply
  5. Jay says

    October 19, 2021 at 1:31 pm

    Handy! Thanks for writing this.

    Reply
  6. Lee says

    September 10, 2022 at 11:36 pm

    Thanks Sal, this worked perfectly first time.

    Reply
  7. Tristan says

    October 17, 2022 at 12:08 am

    I’ve been having issues connecting to Algolia from WP CLI within Local but after many hours and finally finding your article I’ve been able to connect using WP CLI from outside the Local site.

    Thank you so much.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in