Question

Is there a way to get the path to where WordPress is installed?

I was using the following:

$root = realpath($_SERVER["DOCUMENT_ROOT"]);

It is fine for www.example.com -> /usr/local/pem/vhosts/165312/webspace/httpdocs

It is not fine for www.example.com/blog since I need to hard code the folder name (blog).

Later I found a way by using this:

$iroot = getcwd();
$folder = explode("/", $iroot);
$dir = $folder[8]; // I know is 8
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/$dir/wp-blog-header.php";

But still it is a lot of complicated stuff. Is there a simple way to get where WordPress is installed (the path) without hard coding?

Note: 1 WordPress functions will not work since this is somehow outside WordPress. As noted on the last example, the whole point of determine the WordPress installation path is to require "wp-blog-header.php"; on multiple WordPress installations where each installation uses a different folder (example.com/blog-one and example.com/blog-two), not WordPress MU or multi site.

Note: 2 If instead of require "$root/$dir/wp-blog-header.php"; I use require "wp-blog-header.php"; it will work as long as the file is in the same folder, but in my case the file will be sometimes in a different folder.

Was it helpful?

Solution 3

Root example:

  • /usr/local/pem/vhosts/165312/webspace/httpdocs/blog-one
  • /usr/local/pem/vhosts/165312/webspace/httpdocs/blog-two
  • /usr/local/pem/vhosts/165312/webspace/httpdocs/some-blog

The example below will work on any of the blogs ("blog-one", "blog-two" and "some-blog") and the script or file can be installed on any subfolder of the blog.

$current_path = getcwd(); // Get the current path to where the file is located
$folder = explode("/", $current_path); // Divide the path in parts (aka folders)
$blog = $folder[8]; // The blog's folder is the number 8 on the path

// $root = path without the blog installation folder.
$root = realpath($_SERVER["DOCUMENT_ROOT"]);

// Now I can require any WordPress file
require "$root/$dir/wp-blog-header.php";

// For the current installation
// For example, wp-blog-header.php to get the blog name or
// wp-config.php to access the database.

This makes the script independent. The script will work on any folder on any WordPress installation installation as long as the folder is the number 8. If the installation is on a sub folder, the number 8 will have to be increased. Also bear in mind that the current path may have more or fewer folders, meaning that the script has to be adapted accordingly.

Note: This will work by "hard coding" the folder position on the path and, as long as all installation have the same position, the script will work. The other way around is to hard code.

OTHER TIPS

Use the ABSPATH constant:

<?php echo ABSPATH ?>

This should print your WordPress path.

Here's what I'm doing:

Setup:

$docRoot = $_SERVER['DOCUMENT_ROOT'];
$scriptName = $_SERVER['SCRIPT_NAME'];
$queryArray = explode("/", $scriptName);
$queryLength = count($queryArray);

Usage:

require_once($docRoot . ($queryLength > 2 ? "/".$queryArray[$queryLength - 2] : "" ) . '/wp-blog-header.php');

This is working very well for me at the moment for instantiating a WordPress environment regardless of where the script is. Definitely need to sanitize before using any $_SERVER variables (and I need to triplecheck this outside my localhost environment), but I don't think it requires severe modifications to be a practical universal solution.

If all you are trying for is the current directory of where the file being called is located, then I would use:

realpath(dirname(__FILE__))

A lot of this depends on where the file is located. If it is in the same directory as the WordPress installation then it would be something like:

<?php
    $root = realpath(dirname(__FILE__));
    require "$root/wp-blog-header.php";
?>

This is how to do it from within WordPress (which isn't what the questioner was asking for, but what I suspect many people will be searching for…)

There's a dedicated function, get_home_path(), which'll give you something like:

/srv/www/my-site/htdocs/

Other functions for file and content directories (and web paths).

If you can reach the database, you need to do

update_option('wp_dir',dirname(__FILE__));

in file wp-blog-header.php.

Then connect to the database from any application and retrieve the path with the following query:

select option_value from wp_options where option_name ='wp_dir';

Of course, you would need to know the database name and WordPress table prefix.

PS: This may or may not be your solution, but it is one of the ways.

Why not simply do the following in the wp-config.php:

$WP_PATH = implode("/", (explode("/", $_SERVER["PHP_SELF"], -1)));

e.g.

$WP_PATH = implode("/", (explode("/", "/one/two/three/index.php", -1)));

will return "/one/two/three" and

$WP_PATH = implode("/", (explode("/", "/index.php", -1)));

will return ""

$WP_PATH = implode("/", (explode("/", $_SERVER["PHP_SELF"], -4)));

Will give you the exact WordPress root.

For example, your root address is www.myroot.com, and your WordPress site is in the WordPress folder in public_html, then wwww.myroot.com/wordpress/ will be the result in $WP_PATH. In such a case your current file name should be in your plugin root directory. Like some_plugin is my plugin name. So the full path will become:

localhot/wordpress/wp_content/plugins/some_plugin/

And localhot/wordpress/wp_content/plugins/some_plugin/myfile.php is my file name where I wrote this script, will give me /wordpress. This worked for me as I wanted my wordpress directory name. So look for contents like this:

$WP_PATH = implode("/", (explode("/", $_SERVER["PHP_SELF"], -4)));
require_once( $_SERVER['DOCUMENT_ROOT'].$WP_PATH '/wp-load.php');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top