ABSPATH or __FILE__?


Question

Can someone tell me if either of these two methods has an advantage over the other and why?

$mydir = ABSPATH.'/wp-content/themes/mytheme/images';

$mydir = dirname(__FILE__).'/images';

They can both be used to obtain and absolute path to the images directory of "mytheme" regardless of structure of whether wordpress is installed on the root directory or in a subdirectory off the root. In both cases, they are being called from the functions.php file which is located under the "mytheme" folder.

1
16
3/4/2013 5:22:14 AM

Accepted Answer

I would personally prefer dirname() as it is always guaranteed to give me the correct result, while the ABSPATH method relies on a fixed theme path and theme name that can both change.

By the way, you can use __DIR__ instead of dirname(__FILE__).

18
7/3/2014 8:05:43 PM


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon