Skip to content

Instantly share code, notes, and snippets.

@g105b
Created April 29, 2016 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g105b/7575e736458cedd37807d18605a87ff3 to your computer and use it in GitHub Desktop.
Save g105b/7575e736458cedd37807d18605a87ff3 to your computer and use it in GitHub Desktop.
<?php
/**
* Serve Wordpress locally without having to configure a web server.
*
* You need to have the database served as configured in wp-config.php, then
* run php -S 0.0.0.0:8080 wp-router.php to serve, and access localhost:8080 or
* your.computers.ip:8080 in a browser.
*
* Make sure in the database that within the `wp_options` table, the `siteurl`
* and `home` values are both "/".
*/
// Work from the server's document root directory.
$rootDir = $_SERVER["DOCUMENT_ROOT"];
chdir($rootDir);
// Extract path component of incoming request.
$urlComponents = parse_url($_SERVER["REQUEST_URI"]);
$requestedPath = "/" . trim($urlComponents["path"], "/");
// Allow child scripts to use current directory as include path.
set_include_path(implode(":", [
get_include_path(),
__DIR__
]));
$diskPath = rtrim("$rootDir/$requestedPath", "/");
if(file_exists($diskPath)) {
if(is_dir($diskPath) && $requestedPath === "/") {
$requestedPath = rtrim($requestedPath, "/") . "/index.php";
}
if(strstr($requestedPath, ".php")) {
$diskPath = rtrim("$rootDir/$requestedPath", "/");
require_once($diskPath);
}
else {
return false;
}
}
else {
require_once("index.php");
}
@g105b
Copy link
Author

g105b commented Apr 29, 2016

Because sometimes, you have to work with a Wordpress project 👎

@g105b
Copy link
Author

g105b commented Mar 25, 2021

Hi past Greg! Working with a Wordpress thing again today - psh!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment