Is there any function that would return the full path of my plugin in WordPress?
Example is
path/wp-contents/plugins/myplugin
I have tried plugin_dir_path(__FILE__)
but returns the current dir.
Is there any function that would return the full path of my plugin in WordPress?
Example is
path/wp-contents/plugins/myplugin
I have tried plugin_dir_path(__FILE__)
but returns the current dir.
As mentioned by @tsl143 the function
plugins_url()
is what you want, along with the__FILE__
magic constant. If you call it from a file inside your plugin folder it will refer to that folder.Example:
For the directory:
For a file or subdirectory in your plugin directory:
plugin_dir_path( __FILE__ )
will give you plugin path of current file.this is mean if you call this function like that inside "your_plugin_dir/sub_dir/file.php" will return "your_plugin_dir/sub_dir/"
if you want to get the ROOT of your plugin directory, just change
__FILE__
to__DIR__
You can gain it by using this method
Here is a solution, when you are not inside the plugin root:
As of now with 4.7.5, WordPress does not have a get_plugins_root() function like there is a get_theme_root() function. This is probably because you really shouldn't ever need to modify plugins from your theme, and the plugins root directory never changes.
However, it can be useful if you need to programmatically affect plug-ins while developing themes.
Simply, as WP does for the theme root:
Or, if you need a function, why not just do it the same way WordPress does it for the theme root?
Just make a copy of the function get_theme_root() from wp-includes/theme.php and paste it into your theme's functions.php file, rename the function to get_plugins_root(), simplify it, and change 'theme' to 'plugins' in the function...
With the path, you can now add the plug-ins folder name that you wish to affect.
As noted on the section
Common Usage
of Plugins Url function reference page, that's what worked for me:The output for this was:
The file that called the function was my
functions.php
insideincludes
, so the complete path to my file was:Kinda late to this party, but just in case some else stumbles upon this.
plugin_dir_path(__FILE__)
will always return the current path (where the file calling it is located).If you want the root, use the code below:
You can then define a constant: