Created a class in an external script which is dependent on the WordPress environment.
Since it’s a class I intend to use on various projects, I decided to dynamically create a path to wp-blog-header.php and here’s what I came up with. It currently works for me, but I need to ensure that its foolproof.
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$scriptName = $_SERVER['SCRIPT_NAME'];
$queryArray = explode("/", $scriptName);
$queryLength = count($queryArray);
require_once($docRoot . ($queryLength > 2 ? "/".$queryArray[$queryLength - 2] : "" ) . '/wp-blog-header.php');
Does anyone have a better solution or is this good enough to rely on, regardless of script location or WP installation setup?
In the generic case, there is no performant solution other than to check every file and folder that is publicly accessible, and then all the parent folders.
Since this is not a feasible or excusable operation to perform on every page load or request, you’re left with two other options:
The latter is what you’ll be relying on if you want to automate things.
Issues I see with your code:
If you’re okay with those assumptions, then yes, your code is safe to use
However I would recommend you roll your external script into WordPress as a plugin.
As per the Codex,
http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
Whether or not your means and method is foolproof is entirely dependent upon your setup and the locations in which you are placing your scripts, relative to your WordPress installation.
Assuming you follow the convention outlined in your code, then you should be OK.
You should provide a more detailed example of script locations (including any possible variations), versus WordPress location, so if someone has something to add that can refine your code, it’d then make sense, as your snippet is to localized to your use case.
UPDATE#
(in response to your comment)
The path to your WordPress installation is a constant, so instead of devising some trickery to figure out where that path lay, state it as is and in full, just like the example above.
If for example your actual path (for localhost) is something like;
…then,
Will locate your document root, for which you then specify the your install directory and
wp-blog-header.php
file.I would then place a
.htaccess
file inc:apachehtdocs
which amongst other things should include the following,This will include (auto_prepend) the file
auto_inc.php
before any other PHP file so if you have a directory structure like,The
auto_inc.php
file will be included in any of the sub-directories, no matter depth.Now within this automatically included file, you can place a function that wraps the
require('/the/path/to/your/wp-blog-header.php');
for which you can arbitrarily use within your various projects, for example;Or alternatively, you need not wrap the
require
at all and have it be available to you on every file, sub-directory, at any depth, without ever having to specify it again, allowing you to focus on your projects.Seeing as the
/wordpress/
directory already has a.htaccess
of its own, it won’t be affected by the.htaccess
found within its root directory, removing any clash between declaring functions twice.You just need the followingcode to apply to get real path for the
wp-blog-header.php
BTW, if you want to include worpdress core (+theme functions), its better to user:
and NOT