My plugin uses the following code to reference a file, but I've read WP_PLUGIN_DIR won't work if a user renames the default plugin folder. I would also like to replace /location-specific-menu-items/with a reference to the current plugin folder.

$gi = geoip_open(WP_PLUGIN_DIR ."/location-specific-menu-items/GeoIP.dat", GEOIP_STANDARD);

How could I rewrite this to make it work regardless of the names of the WP plugin directory and the specific plugin folder?


Here is my final working solution following everyone's input. Many thanks!

$GeoIPv4_file = plugin_dir_path( __FILE__ ) . 'data/GeoIPv4.dat';
$GeoIPv6_file = plugin_dir_path( __FILE__ ) . 'data/GeoIPv6.dat';

if (!filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {     
    if ( is_readable ( $GeoIPv4_file ) ) { 
        $gi = geoip_open( $GeoIPv4_file, GEOIP_STANDARD );
        $user_country = geoip_country_code_by_addr($gi, $ip_address);
} elseif (!filter_var($ip_address, FILTER_VALIDATE_IP,FILTER_FLAG_IPV6) === FALSE) {
    if ( is_readable ( $GeoIPv6_file ) ) {
        $gi = geoip_open( $GeoIPv6_file, GEOIP_STANDARD );
        $user_country = geoip_country_code_by_addr($gi, $ip_address);
} else {
    $user_country = "Can't locate IP: " . $ip_address;              
  • where did you read that..? if the user renames the plugin folder I think they would have to be sure to redefine WP_PLUGIN_DIR as well...
    – majick
    Feb 12, 2016 at 11:43
  • Ah, I misspoke. The problem is that plugins don't always live in the default plugins directory. Here's the quote I read: "Don't use WP_PLUGIN_URL or WP_PLUGIN_DIR — plugins might not be in the plugins directory."
    – j8d
    Feb 12, 2016 at 15:01

3 Answers 3


If the plugin structure is:


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.

  • 1
    ps: note that if you use plugin_dir_path( __FILE__ ), then it's a wrapper for trailingslashit( dirname( __FILE__ ) ) that adds a trailing slash to the dirname.
    – birgire
    Feb 12, 2016 at 11:47
  • woah cool that is almost exactly word for word what I just posted, lol
    – majick
    Feb 12, 2016 at 11:50
  • hehe I just beat you to it, but this can happen, anyway +1 ;-)
    – birgire
    Feb 12, 2016 at 12:01
  • So $file = plugin_dir_path( __FILE__ ) . 'data/GeoIP.dat'; would work was well?
    – j8d
    Feb 12, 2016 at 15:12
  • 1
    yes that should work @j8d
    – birgire
    Feb 12, 2016 at 15:13

You can use:


Which as is just a wrapper function anyway for:


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.

  • I don´t see a problem with using plugin_dir_url(), it returns the folder for the file you passed, yeah. That´s what he needs.
    – flomei
    Feb 12, 2016 at 11:54
  • 1
    plugin_dir_path returns the directory, plugin_dir_url will give you the URL to the file, which is simply not the same thing and NOT what he needs. it is useful for other things - like image sources or enqueueing stylesheets though.
    – majick
    Feb 12, 2016 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.