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 to
dirname(__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.

Leave a Comment