WordPress – Get root directory?

How can I get the root directory of my site? I want to use it as the src for <img>.

For example If I install the wordpress on www.mysite.com/blog/, the root is /blog.

Read More

But If I install it to www.mysite.com/test/blog, the root is /test/blog.

I tried to use the snippet from this question, but it returns the whole path as seen in FTP. So instead of returning just /blog, it returns /home/public_html/blog which won’t work if used as src in image tag.

Any solution? Thanks.

Related posts

Leave a Reply

3 comments

  1. site_url() return the path with http, in some cases, it is not allowed if the server turns off url inclusion. You can use the function below to get the root path of wordpress installation folder:

    function get_wp_installation()
    {
        $full_path = getcwd();
        $ar = explode("wp-", $full_path);
        return $ar[0];
    }