1

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

my-plugin/includes 

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

  • 2
    define(PLUGIN_NAME, "MY PLUGIN NAME"); is enough. Why do you need something dynamically that's not dynamic at all. – Mayeenul Islam Apr 27 '15 at 9:45
  • because I suppose that the user might install the plugin into a directory of his choice. – Tetravalente Apr 27 '15 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. – Pieter Goosen Apr 27 '15 at 10:14
  • I mean that the user might install the plugin into plugins/theplugins rather than plugins/myplugin – Tetravalente Apr 27 '15 at 10:23
  • I don't understand your issue. Reread my previous comment. – Pieter Goosen Apr 27 '15 at 10:26
6

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 – Jacob Raccuia Jan 16 '18 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 '18 at 12:51
  • Make sure that the main comment in your plugin file contains the relevant data as per codex.wordpress.org/File_Header – Mark Howells-Mead Apr 16 '18 at 17:35
1

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.

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.