Using Tinkerwell with WordPress and Local by Flywheel

Making two great solutions work together.

In my last post, I've ˙praised the uncelebrated power of the WP-CLI shell command.

WP-CLI maintainer Alain Schlesser provided a further tip to make the WP-CLI shell even better through the schlessera/wp-cli-psysh package. I strongly suggest you try that out.

Another tool I use for my daily work is the Tinkerwell application.

Tintkerwell is a step up from an interactive PHP shell to an elementary code editor. Tinkerwell UI does not replace a proper PHP IDE, but it provides a UI with some neat features (vim mode, for one) with a clean and straightforward approach.

I've found this article and videos by Ross Wintle enjoyable and easy to follow and suggest you give it a read and look. I could set up Tinkerwell to work on my local WordPress site in little time, and you will be able to do the same if your WordPress application is served on localhost.

Should that not be the case, e.g., if you're using Local by Flywheel or some other "containerized" solution, then setting Tinkerwell up might require either its remote connection function to do some modification to the WordPress installation configuration file.

Setting up a Local site for Tinkerwell

Since the SSH-based approach seems overkill and too complicated for the simple needs of tinkering on a local site, I prefer the second approach and consistently update my Local by Flywheel websites wp-config.php file to correctly handle requests coming from the Tinkerwell application.

First of all, set the Tinkerwell working directory to the app/public directory of your Local by Flywheel WordPress site:

Set up Tinkerwell working directory

Second, modify the site wp-config.php file to detect an incoming request from the Tinkerwell application and connect to the WordPress database via the correct database host:

/** MySQL hostname */
if ( isset( $_SERVER['SCRIPT_NAME'] ) 
	&& false !== stripos( $_SERVER['SCRIPT_NAME'], 'tinkerwell' ) ) {
	// Host from the Local > Database > "Remote Host" field.
	$db_host = '';
	// Port from the Local > Database > "Remote Port" field, omit if there's no port.
	$db_port = ':4026';
	define( 'DB_HOST', "{$db_host}{$db_port}" );
} else {
	define( 'DB_HOST', 'localhost' );

I use the previous version of Local, the Docker-based one, but the snippet will work with the new, MAMP-like version.

If everything is correct, you should be able to start tinkering with your WordPress site using Tinkerwell.

If you're using more elaborate solutions, e.g, a Docker stack, then just set the $db_host and $db_port variables to the database container IP address and port. Chances are, if you've got no idea what this means, then you're probably not using a container-based solution and the snippet and example above should be fine.