Skip to content

Instantly share code, notes, and snippets.

@MouseEatsCat
Last active October 7, 2025 20:18
Show Gist options
  • Save MouseEatsCat/dfc2090b68f2effa02c42a5b8d3f75fc to your computer and use it in GitHub Desktop.
Save MouseEatsCat/dfc2090b68f2effa02c42a5b8d3f75fc to your computer and use it in GitHub Desktop.

Locomotive Bedrock (Wordpress) Valet Driver

This adds Charcoal project support for Laravel Valet.

The driver will match any Bedrock project with a public directory of web or www. It support having either wp or wordpress as the wordpress install directory.

Setup

  • Run the following command to place the CharcoalValetDriver.php driver file into ~/.config/valet/Drivers/.
curl https://gist.github.com/raw/dfc2090b68f2effa02c42a5b8d3f75fc/LocomotiveBedrockValetDriver.php -o ~/.config/valet/Drivers/LocomotiveBedrockValetDriver.php
<?php
use Valet\Drivers\BasicValetDriver;
class LocomotiveBedrockValetDriver extends BasicValetDriver {
const PUBLIC_PATHS = ['www', 'web'];
const WP_PATHS = ['wp', 'wordpress'];
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri)
{
foreach (static::PUBLIC_PATHS as $publicPath) {
if (
file_exists("$sitePath/$publicPath/mu-plugins/bedrock-autoloader.php") ||
(is_dir("$sitePath/$publicPath/") &&
file_exists("$sitePath/$publicPath/wp-config.php") &&
file_exists("$sitePath/config/application.php"))
) {
return true;
}
}
return false;
}
/**
* Determine if the incoming request is for a static file.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string|false
*/
public function isStaticFile($sitePath, $siteName, $uri)
{
foreach (static::PUBLIC_PATHS as $publicPath) {
$staticFilePath = $sitePath.'/'.$publicPath.$uri;
if ($this->isActualFile($staticFilePath)) {
return $staticFilePath;
}
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
public function frontControllerPath($sitePath, $siteName, $uri)
{
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
foreach (static::PUBLIC_PATHS as $publicPath) {
if (is_dir("$sitePath/$publicPath/")) {
$path = parent::frontControllerPath(
$sitePath.'/'.$publicPath,
$siteName,
$this->forceTrailingSlash($uri)
);
foreach (static::WP_PATHS as $wpPath) {
if (is_dir("$sitePath/$publicPath/$wpPath")) {
if (! str_contains($_SERVER['PHP_SELF'], "$wpPath/wp-admin")) {
$_SERVER['PHP_SELF'] = '/index.php';
}
}
}
return $path;
}
}
}
/**
* Redirect to uri with trailing slash.
*
* @param string $uri
* @return string
*/
private function forceTrailingSlash($uri)
{
foreach (static::WP_PATHS as $wpPath) {
$substr = substr($uri, -1 * strlen("/$wpPath/wp-admin"));
if ($substr == "/$wpPath/wp-admin") {
header('Location: '.$uri.'/');
exit;
}
}
return $uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment