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.
Unfortunately, most of the answers here seem to forget about one important thing.
In addition to the plugins dir, plugins might be also in the Must-use plugins (mu-plugins) directory
Because of that, we need to check multiple directories.
An example function to do this:
My solution has been to use inside plugin_dir_path DIR
The above should give you the absolute path to your plugin folder on the server, then you can load your variable with any file within your plugin folder
plugins_dir_path()
is a misnomer.It will always return the path to the current file.
Link to the codex:
https://developer.wordpress.org/reference/functions/plugin_dir_path/#more-information
It is actually synonym for the
trailingslashit()
function.IF the current file is in the
plugins
directory, then yes, the function returns the path of the current file.However, if you call this function from a file inside any other directory, then current file is not in the plugins directory, and thus it will does NOT return the path to the plugins directory. It does always return the path to the current file, but without a trailing slash.
Most of the answers here are incorrect, or are only "sometimes" correct. Those answers will only work as they say IF your file happens to already be located in the plugins directory! In all other cases they will give you a misleading result: the path to your current file.
It is more likely that your file is in a *subdirectory *of the plugins folder.
If this is the case, this codex shows you how to create a URL to the current file: https://codex.wordpress.org/Function_Reference/plugins_url
You would then need to remove your file name to get the path. Or use ``
ANSWER:
These solutions solutions are independant of where the file of your calling function is located. If your file is located in the plugins folder, or a subdirectory of it, then the above options would work. Otherwise, you'll need to resort to something along the lines of:
WP_PLUGIN_DIR
WordPress does have a constant defined, for the Plugins' folder:
Codex: https://codex.wordpress.org/Determining_Plugin_and_Content_Directories
Or, If you can guarantee that the plugins folder is located in the normal place for a WordPress install, i_a's answer above answer above will work, no matter what directory your file (that you want to call this function from) is in. Please see his more complete post in this thread, but so as to not have a "link only answer", I'll include here that it Essentially, it boils down to using the following (and turning it into a function):
Or M07's post, also in this thread, here: https://stackoverflow.com/a/28525164/5411817
You can define a variable into your main Php file. It should be at the root of your plugin folder. The file should be here : .../wp-content/plugins/plugin-folder/my-plugin.php
You can add into the file this line.
After you can use your new variable anywhere into your plugin.
I hope it will help someone.
I would suggest to use a WordPress internal constant to solve this case:
Alternative
The other valid alternative is to compute the path from the URL which is more complex/confusing. I would not use this code:
My opinion in this case is: Use the constant
WP_PLUGIN_DIR
Yeah as per description of plugin_dir_path it will give you current plugin file path. But as per what you asking here you can do something like below unfortunately no direct way,