7

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.

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.

Community
  • 1
  • 1
hrsetyono
  • 4,474
  • 13
  • 46
  • 80

3 Answers3

14

You may use site_url() (eventually with echo) to get absolute path with server name and directory. Also, have a look at wordpress documentation about similar functions & what they provide.

sk001
  • 561
  • 6
  • 27
mrówa
  • 5,671
  • 3
  • 27
  • 39
  • Wow `site_url()` does the trick. Though, it gives the full path like `http://mysite.com/blog`. But I guess it's okay. – hrsetyono May 28 '13 at 03:12
7

You may have better luck over at the Wordpress Stack Exchange site :)

And this suggestion to use ABSPATH didn't help on that thread? https://stackoverflow.com/a/2356467/413254

Community
  • 1
  • 1
loeschg
  • 29,961
  • 26
  • 97
  • 150
2

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];
}
Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20