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.
To find your WordPress root directory from your cPanel dashboard, first, log in to your cPanel and then find the 'File Manager' as shown below. From here, you will be able to access all the files which have been installed or used on your website inside the 'public_html' folder.
In order to get the root directory path, you can use _DIR_ or dirname(). echo dirname(__FILE__); Both the above syntaxes will return the same result.
The domain root folder is typically called html, www, or public_html on most web hosts. Some WordPress installs are pretty gnarly with folders inside of folders inside of folders, and finding the folder that the host considers to be the primary domain root folder can take some digging.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With