Just a quick one for WordPress devs out there trying to set up WP-CLI on a Windows Machine.  This assumes you’re using, or want to use a WAMP stack for local development.  This article also highlights some tools you can use to set up a WordPress installation on a WAMP stack quickly and easily.

Setting up your WAMP stack

If you don’t already have a WAMP stack set up, you can do so by installing Apache, MySQL and PHP separately or you can use something like XAMPP, which takes care of all the hard work and configuration for you.  If you also want an automated WordPress installation, you can download a Bitnami WordPress package for XAMPP which provides you with a tidy little installer to make setting up WordPress a breeze.  The Bitnami WordPress package also conveniently happens to include WP-CLI (in  C:/xampp/apps/wordpress/bin/ assuming you installed at the default location on the C: drive).

Setting up WP-CLI

If you’ve already got your WAMPP stack or decided to take a different approach, you may need to install WP-CLI manually, which should yield a wp-cli.phar file.

Setting up Git Bash for Windows with WP-CLI

If you don’t have it already, you’re going to want to get yourself Git for windows.  This comes with a Git Bash tool for windows which can be launched from the right click menu or from the Git Bash executable.  When you have it go ahead and launch it.

Next, we’ll set up a bash profile for your current user account in Git Bash, create an alias, allowing you to properly run wp-cli.phar.  Lets go ahead and create your bash profile in your home directory by launching git bash and executing touch ~/.bash_profile && nano ~/.bash_profile, then add your alias.  Don’t judge my use of nano, I prefer editors I can get out of.

alias wp="/c/xampp/php/php.exe /c/xampp/apps/wordpress/bin/wp-cli.phar"

You can replace wp with whatever command you want to use to run wp-cli, the first path in the string should be the path to your PHP executable, the second should be the path to your wp-cli.phar file.

Adding MySQL binaries to $PATH

Next you’ll need to add your mysql binaries to your $PATH variable so that WP CLI can call upon them to interact with the WordPress database, again you can add this to your bash profile.  These are found in the bin/ directory of your mysql installation, if you’re using xampp.  If you went with xampp, you’ll find your mysql binaries in its installation directory (C:/xampp/mysql/bin/ if you went with the default location).

# Add mysql binaries executable to bath
PATH=$PATH:/c/xampp/mysql/bin/

So all together your bash profile should look like this:

alias wp="/c/xampp/php/php.exe /c/xampp/apps/wordpress/bin/wp-cli.phar"

# Add mysqldump executable to bath
PATH=$PATH:/c/xampp/mysql/bin/

Once you’ve done this and saved your changes, don’t forget to source your profile with source ~/.bash_profile.

You should now be able to execute wp-cli from Git Bash using the wp alias in your terminal.  Try executing wp cli version, if everything went according to plan you should see something similar to the following:

User@PC-Name XXXXX64 /c/xampp/apps/wordpress/bin
wp cli version
WP-CLI 2.2.0

If you want to test that your git bash can interact with mysql, try exporting your WordPress database with:

User@PC-Name XXXXX64 /c/xampp/apps/wordpress/htdocs
wp db export
Success: Exported to 'bitnami_wordpress-2019-07-09-7d24370.sql'
Hi I'm James, I am a Software Engineer working at Veygo and I experiment with Machine Learning in my spare time. My current focus is on WordPress and PHP, but I've also worked with Python, React, C++, C# and Java.

Leave a Reply

avatar
  Subscribe  
Notify of