Last Updated: February 25, 2016
· merianos

Find the WordPress root directory from any child directory by using PHP

Today I try to built a TinyMCE plugin with a custom dialog, and I like to dialog to be integrated with the WordPress APIs.

In order to find out the WordPress root directory I have write the following function

function get_wp_root()
    $depth          =   0;
    $current_path   =   __DIR__;

    while(!file_exists($current_path . DIRECTORY_SEPARATOR . 'wp-config.php'))
        $current_path   =   dirname($current_path);

        if($depth > 20)

    return $current_path;

I hope this helps :)

1 Response
Add your response


WordPress has the ABSPATH constant, which it sets in wp-config.php, which stores the path to the WordPress root directory. Is this what you're trying to access?

over 1 year ago ·