Wordpress - Best Practice for Referencing the Plugin Directory
If the plugin structure is:
plugins/
some-plugin/
some-plugin.php
data/
GeoIP.dat
then for PHP 5.3.0+, you could try the magic constant __DIR__
__DIR__
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent todirname(__FILE__)
. This directory name does not have a trailing slash unless it is the root directory.
within the some-plugin.php
file:
// Full path of the GeoIP.dat file
$file = __DIR__ . '/data/GeoIP.dat';
// Open datafile
if( is_readable ( $file ) )
$gi = geoip_open( $file, GEOIP_STANDARD );
For wider PHP support you could use dirname( __FILE__ )
, where __FILE__
was added in PHP 4.0.2.
You can use:
plugin_dir_path(__FILE__);
Which as is just a wrapper function anyway for:
trailingslashit(dirname(__FILE__));
You could also have a look at the functions WordPress has on board for this: e.g. plugin_dir_path()
, plugins_url()
or plugin_dir_url()
They will help you with determining where your plugin is placed on the server. Those functions are also recommended by the Codex in Writing a Plugin: Names, Files, and Locations.
Besides that you can obviously use magic constants from PHP and filtering their output to determine where your files are.