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.
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.
Since WP 2.6.0 you can use plugins_url()
method.
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.
If you want to get current directory path within a file for that you can magic constants __FILE__
and __DIR__
with 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 current directory path.
If you want to one level up from the current directory. You should use __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.
This will actually get the result you want:
<?php plugin_dir_url(__FILE__); ?>
http://codex.wordpress.org/Function_Reference/plugin_dir_url
$full_path = WP_PLUGIN_URL . '/'. str_replace( basename( __FILE__ ), "", plugin_basename(__FILE__) );
This link may help: http://codex.wordpress.org/Determining_Plugin_and_Content_Directories.
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
Source: Stackoverflow.com