48

I need to get the current plugin directory like:

[wordpress_install_dir]/wp-content/plugins/plugin_name

(If getcwd() is called from the plugin, it returns [wordpress_install_dir], the root of installation.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bog
  • 485
  • 1
  • 4
  • 5

11 Answers11

91

Use the WordPress core function that's designed specifically for that purpose:

<?php plugin_dir_path( __FILE__ ); ?>

See the Codex documentation here.

You also have

<?php plugin_dir_url( __FILE__ ); ?>

if what you're looking for is a URI as opposed to a server path.

See the Codex documentation here.

IMO, it's always best to use the highest-level method that's available in core, and this is it. It makes your code more future-proof.

Keep in mind that if you call these functions in a file that is in a subdirectory of your plugin, the result will include the subdirectories. It may be useful to define a constant in your root plugin file using these functions.

Peter
  • 13,733
  • 11
  • 75
  • 122
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
  • 8
    Further, if you're trying to get at a resource in a location that's relative to that plugin's directory, use `plugins_url( 'images/image_inside_plugin_folder.png' , __FILE__ )` – Tom Auger Oct 31 '11 at 16:04
  • 3
    This is the correct answer.WP_PLUGIN_URL will not work if plugin is being used as a MU (must use) plugin, while plugin_dir_path() and plugin_dir_url() will. – Andy Dec 29 '11 at 04:59
  • Thanks for passing on the codex doc. I found __FILE__ did the trick for me. – Ian Jun 17 '12 at 08:13
  • @Andy if you have an mu-plugin and are using the load.php method, then plugin_dir_url() resolves to wp-content/mu-plugin not wp-content/mu-plugin/your-plugin-name so your comment is only partially correct. – Josh J Aug 26 '14 at 15:28
  • 5
    Despite the name, `plugin_dir_path` doesn't necessarily get the plugin directory, it gets the parent directory of the path passed as the first argument. So if `__FILE__` is not in the plugin directory, `plugin_dir_path( __FILE__ )` will not return the plugin directory. See [more information on `plugin_dir_path`](https://developer.wordpress.org/reference/functions/plugin_dir_path/#more-information) – Flimm Oct 11 '17 at 10:58
  • I am getting forbidden access when trying to use `plugin_dir_url`. – Jovanni G Apr 26 '18 at 22:27
43

Looking at the OP's own answer, I think the OP wants;

$plugin_dir_path = dirname(__FILE__);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TheDeadMedic
  • 9,948
  • 2
  • 35
  • 50
  • 14
    In PHP 5.3 you can use the new constant `__DIR__`, which achieves the same thing. – dave1010 Jun 09 '11 at 09:31
  • 2
    You're almost there. Heck, you've even named the variable the same as the built-in WordPress function that does what you want. Future-proof your code by using the recommended WordPress function to do what you want. See my answer. – Tom Auger Apr 03 '12 at 14:35
  • @TomAuger `plugin_dir_path( $file )` is merely a wrapper for `trailingslashit( dirname( $file ) )` - I would argue it's no more "future proof" than mine; the only difference is a trailing slash. – TheDeadMedic Apr 04 '12 at 10:39
  • 3
    No, it is more future proof because if the core team for some reason decides to change the architecture or the mechanic, they will modify the plugin_dir_path() function to reflect this change, whereas the direct call to dirname( $file ) would then be stranded. If a function in core exists, use it, even if it just appears to be a meaningless wrapper. – Tom Auger Apr 04 '12 at 14:56
  • 1
    Don't get me wrong, I understand it's the "correct" WordPress way. But let's be clear; the function's sole intent is to return the absolute trailing-slashed path to the directory of a given file. Even if it was modified, or the WP filesystem changed, it would still need to return the equivalent of `dirname( __FILE__ ) . '/'`. Anything else would compromise the functionality of any plugin using it. – TheDeadMedic Apr 05 '12 at 12:51
  • 5
    this is only valid if the file calling it is IN the root directory of the plugin. If, for example, you have sub folders within the plug, and you call it from within one of those folders, you'll end up with the path to that subfolder! – Dave Amphlett Oct 11 '13 at 14:07
14

This will actually get the result you want:

<?php plugin_dir_url(__FILE__); ?>

plugin_dir_url(string $file)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brenjt
  • 15,997
  • 13
  • 77
  • 118
8

To get the plugin directory you can use the WordPress function plugin_basename($file). So you would use it as follows to extract the folder and filename of the plugin:

$plugin_directory = plugin_basename(__FILE__);

You can combine this with the URL or the server path of the plugin directory. Therefore you can use the constants WP_PLUGIN_URL to get the plugin directory URL or WP_PLUGIN_DIR to get the server path. But as Mark Jaquith mentioned in a comment below this only works if the plugins resides in the WordPress plugin directory.

Read more about it in the WordPress codex.

Peter
  • 13,733
  • 11
  • 75
  • 122
stefanglase
  • 10,296
  • 4
  • 32
  • 42
6
$full_path = WP_PLUGIN_URL . '/'. str_replace( basename( __FILE__ ), "", plugin_basename(__FILE__) );
  • WP_PLUGIN_URL – the URL of the plugins directory
  • WP_PLUGIN_DIR – the server path to the plugins directory

This page may help: Determining Plugin and Content Directories

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
manish nautiyal
  • 2,556
  • 3
  • 27
  • 34
2

Try this:

function PluginUrl() {

    // Try to use the WordPress API if possible, introduced in WordPress 2.6
    if (function_exists('plugins_url'))
        return trailingslashit(plugins_url(basename(dirname(__FILE__))));

    // Try to find it manually... can't work if wp-content was renamed or is redirected
    $path = dirname(__FILE__);
    $path = str_replace("\\", "/", $path);
    $path = trailingslashit(get_bloginfo('wpurl')) . trailingslashit(substr($path, strpos($path, "wp-content/")));
    return $path;
}

echo PluginUrl(); will return the current plugin URL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pennywise83
  • 1,784
  • 5
  • 31
  • 44
1

Since WordPress 2.6.0, you can use the plugins_url() method.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Idham Perdameian
  • 2,199
  • 24
  • 34
1

When I need to get the directory, not only for the plugins (plugin_dir_path), but a more generic one, you can use __DIR__. It will give you the path of the directory of the file where is called. Now you can used from functions.php or another file!

Description:

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. [1]1

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

If you want to get the current directory path within a file, you can use the magic constants __FILE__ and __DIR__ with the plugin_dir_path() function as:

$dir_path = plugin_dir_path( __FILE__ );

CurrentDirectory Path:

/home/user/var/www/wordpress_site/wp-content/plugins/custom-plugin/

__FILE__ magic constant returns the current directory path.

If you want to one level up from the current directory, you should use the __DIR__ magic constant as:

Current Path:

/home/user/var/www/wordpress_site/wp-content/plugins/custom-plugin/

$dir = plugin_dir_path( __DIR__ );

One level up path:

 /home/user/var/www/wordpress_site/wp-content/plugins/

__DIR__ magic constant returns one level up directory path.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
0

Within the plugin's main PHP file, this only works in admin:

$plugin_data = get_plugin_data( __FILE__ );
$plugin_name = $plugin_data['Name'];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Malki Mohamed
  • 1,578
  • 2
  • 23
  • 40
0

Define a constant or a function in the plugin's main file with the __DIR__ magic constants. In PHP, the __DIR__ is used to obtain the current code working directory. If you call the constant or function from other places, it will return your plugin directory path. Take a look at the example below of a constant that holds your current plugin directory path.

define( 'YOURPLUGIN_PATH', __DIR__ );
Eh Jewel
  • 619
  • 8
  • 14