Take the 2-minute tour ×

I need to get the current plugin directory like [wordpress_install_dir]/wp-content/plugins/plugin_name

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

thanks for help

share|improve this question
add comment

6 Answers

up vote 22 down vote accepted

Looking at your own answer @Bog, I think you want;

$plugin_dir_path = dirname(__FILE__);
share|improve this answer
In PHP 5.3 you can use the new constant __DIR__, which achieves the same thing. –  dave1010 Jun 9 '11 at 9:31
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 3 '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 4 '12 at 10:39
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 4 '12 at 14:56
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 5 '12 at 12:51
show 1 more comment

Why not use the WordPress core function that's designed specifically for that purpose?

<?php plugin_dir_path( __FILE__ ); ?>

See 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 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.

share|improve this answer
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
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 4:59
Thanks for passing on the codex doc. I found FILE did the trick for me. –  Ian Jun 17 '12 at 8:13
add comment

This will actually get the result you want:

<?php plugin_dir_url(__FILE__); ?>


share|improve this answer
This returns a URL not a server path name. Although handy in some cases, not really an answer to the question. –  Luke Mar 9 '13 at 1:48
add comment

To get the plugin directory you can use the Wordpress function plugin_basename($file). So you would use is 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. Therefor 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.

share|improve this answer
this is not the answer –  bog Jul 1 '10 at 15:56
Don't use WP_PLUGIN_URL or WP_PLUGIN_DIR — plugins might not be in the plugins directory. –  Mark Jaquith Aug 20 '11 at 4:31
Thanks, I added it to my answer. –  stefanglase Aug 20 '11 at 10:20
add comment
  • WP_PLUGIN_URL – the url of the plugins directory
  • WP_PLUGIN_DIR – the server path to the plugins directory

This link may help: http://codex.wordpress.org/Determining_Plugin_and_Content_Directories.

share|improve this answer
add comment

Try this:

function PluginUrl() {

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

        //Try to find 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.

share|improve this answer
add comment

Your Answer


