Creating a Dynamic Path to wp-blog-header.php

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?

Related posts

Leave a Reply

4 comments

  1. 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:

    • Define the location manually, which is not an unreasonable request
    • Make assumptions and expectations about where WordPress can be found relative to your file

    The latter is what you’ll be relying on if you want to automate things.

    Issues I see with your code:

    • It assumes that WordPress is either in the same folder or 2 folders up

    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.

  2. As per the Codex,

    http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

    <?php
    require('/the/path/to/your/wp-blog-header.php');
    ?>
    

    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;

    c:apachehtdcoswordpress
    

    …then,

    //document root being c:apachehtdocs
    
    $_SERVER["DOCUMENT_ROOT"] . '/wordpress/wp-blog-header.php';
    

    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 in c:apachehtdocs which amongst other things should include the following,

    php_value auto_prepend_file "auto_inc.php" //the auto_inc.php can be a name of choice
    

    This will include (auto_prepend) the file auto_inc.php before any other PHP file so if you have a directory structure like,

    ..htdocs (root)
    ..htdocsproject1
    ..htdocsproject2
    ..htdocsproject-testsample
    ..htdocswordpress
    ..htdocs.htaccess
    ..htdocsauto_inc.php
    ..htdocsindex.php
    

    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;

    function wp_function_include() {
    
    $path = require('/the/path/to/your/wp-blog-header.php');
    
    echo $path;
    
    }
    
    wp_functions_include(); //which fires the verbose require(path..wp-blog-header.php)
    

    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.

  3. You just need the followingcode to apply to get real path for the wp-blog-header.php

    $scriptPath = dirname(__FILE__);
    $path = realpath($scriptPath . '/./');
    $filepath = split("wp-content", $path);
    // print_r($filepath);
    define('WP_USE_THEMES', false);
    require(''.$filepath[0].'/wp-blog-header.php');
    
  4. BTW, if you want to include worpdress core (+theme functions), its better to user:

    require('/wp-load.php');
    

    and NOT

    require('/wp-blog-header.php');