I need to get the current plugin name using a define like this

define(PLUGIN_NAME, plugin_basename(dirname(__FILE__)));

Regrettably, that code doesn't work because the php file is nested inside a subdirectory (includes) of my plugin directory and it returns


Is there any function from Wordpress API to accomplish this task? Thanks in advance.

  • 3
    define(PLUGIN_NAME, "MY PLUGIN NAME"); is enough. Why do you need something dynamically that's not dynamic at all. Apr 27, 2015 at 9:45
  • because I suppose that the user might install the plugin into a directory of his choice. Apr 27, 2015 at 9:54
  • Plugins can only be installed in the mu folder or plugins folder and cannot go into sub directories, so you don't have any thing to worry about. Apr 27, 2015 at 10:14
  • I mean that the user might install the plugin into plugins/theplugins rather than plugins/myplugin Apr 27, 2015 at 10:23
  • I don't understand your issue. Reread my previous comment. Apr 27, 2015 at 10:26

3 Answers 3


Within the plugin's main PHP file:

$plugin_data = get_plugin_data( __FILE__ );
$plugin_name = $plugin_data['Name'];
  • This only works in admin, by the way Jan 16, 2018 at 14:38
  • I like this, but when I output all the associative array keys & values, only the TextDomain gives a value: PluginURI = Version = Description = Author = AuthorURI = TextDomain = my-calculator DomainPath = Network = Title = AuthorName = ... how do I set/retrieve all the other values for #plugin_data ?
    – TARKUS
    Apr 15, 2018 at 12:51
  • Make sure that the main comment in your plugin file contains the relevant data as per codex.wordpress.org/File_Header Apr 16, 2018 at 17:35

you can get plugin name in a subdirectory of your plugin by this snippet code:

$plugin = basename( plugin_dir_path(  dirname( __FILE__ , 2 ) ) );

only you should write level of subdirectory relative to plugin folder in second parameter of dirname() function.


This will give you the plugin folder name regardless of where the file is located and not having to know anything about the directory structure of the plugin.

$plugin_folder_name = reset(explode('/', str_replace(WP_PLUGIN_DIR . '/', '', __DIR__)));

For a break down of what is happening

// Get the relative path to current file from plugin root
$file_path_from_plugin_root = str_replace(WP_PLUGIN_DIR . '/', '', __DIR__);

// Explode the path into an array
$path_array = explode('/', $file_path_from_plugin_root);

// Plugin folder is the first element
$plugin_folder_name = reset($path_array);
  • The single line causes a notice to be thrown "Notice: Only variables should be passed by reference" so I use the long way round version.
    – popnoodles
    Dec 21, 2021 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.