Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ABSPATH or __FILE__?

Tags:

php

wordpress

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.

like image 269
Scott B Avatar asked Dec 25 '09 00:12

Scott B


People also ask

What is the __ file __ in Python?

The __file__ variable: __file__ is a variable that contains the path to the module that is currently being imported. Python creates a __file__ variable for itself when it is about to import a module. The updating and maintaining of this variable is the responsibility of the import system.

What does Abspath mean in Python?

abspath() returns a normalized absolutized version of the pathname path which may sound fancy but it simply means that this method returns the pathname to the path passed as a parameter to this function. Syntax: os.path.abspath(path) Parameter: Path: A path-like object representing a file system path.

What is __ FILE __ Ruby?

In Ruby, the Windows version anyways, I just checked and __FILE__ does not contain the full path to the file. Instead it contains the path to the file relative to where it's being executed from.

What is __ path __ in Python?

If you change __path__ , you can force the interpreter to look in a different directory for modules belonging to that package. This would allow you to, e.g., load different versions of the same module based on runtime conditions.


1 Answers

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__).

like image 194
Pekka Avatar answered Oct 05 '22 08:10

Pekka