Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get WordPress installation folder path

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.

like image 759
user983248 Avatar asked Jun 08 '12 01:06

user983248


1 Answers

Use the ABSPATH constant:

<?php echo ABSPATH ?>

This should print your WordPress path.

like image 183
Estevão Lucas Avatar answered Sep 24 '22 22:09

Estevão Lucas