Laravel Valet 4 custom local WordPress driver for custom WordPress file path
Using valet has been a joy for years, but my custom local valet driver stopped working after updating to the latest valet (4.1) version due to a different driver folder and the use of namespace (fully php 8 mode). So I updated my local driver for WordPress projects where I hold the WordPress files in a separate ‘wp’ folder (to sperate them in the composer septup).. bla bla.. the file:
<?php use Valet\Drivers\Specific\WordPressValetDriver; class LocalValetDriver extends WordPressValetDriver { public function __construct() { $this->subfolder = 'wp'; } /** * Determine if the driver serves the request. */ public function serves(string $sitePath, string $siteName, string $uri): bool { return is_dir($sitePath . '/' . $this->subfolder . '/wp-admin'); } /** * Determine if the incoming request is for a static file. */ public function isStaticFile(string $sitePath, string $siteName, string $uri)/*: string|false */ { $paths = ['wp-admin/css', 'wp-includes']; foreach ($paths as $path) { if (false !== ($pos = stripos($uri, '/' . $path))) { $new_uri = '/' . $this->subfolder . substr($uri, $pos); if (file_exists($sitePath . $new_uri)) { return $sitePath . $new_uri; } } } return parent::isStaticFile($sitePath, $siteName, $uri); } /** * Get the fully resolved path to the application's front controller. */ public function frontControllerPath(string $sitePath, string $siteName, string $uri): string { $_SERVER['PHP_SELF'] = $uri; $_SERVER['SERVER_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; $matched = false; $paths = ['wp-admin', 'wp-includes', 'wp-login']; foreach ($paths as $path) { if (false !== ($pos = stripos($uri, '/' . $path))) { $uri = '/' . $this->subfolder . substr( $uri, $pos ); } } return parent::frontControllerPath( $sitePath, $siteName, $this->forceTrailingSlash($uri) ); } private function forceTrailingSlash(string $uri): string { if (substr($uri, -1 * strlen('/wp-admin')) == '/wp-admin') { header('Location: '.$uri.'/'); die; } return $uri; } }