plugin_basename()
Gets the relative path to the main plugin file (from the plugins or MU plugins directory) based on the provided file path.
Trims the path to the plugins folder from the specified path to the plugin folder/file. Leaves the path from the plugin folder to the specified plugin file inclusive.
The path is obtained without a leading slash: my-plugin/my-plugin.php.
Plugin from a single PHP file
For a plugin that consists of a single PHP file and is located directly in the folder:
plugins, for example:wp-content/plugins/plugin.php- mu-plugins, for example:
wp-content/mu-plugins/plugin.php
Then plugin_basename( __FILE__ ) will return plugin.php.
Uses constants: WP_PLUGIN_DIR and WPMU_PLUGIN_DIR. It is not recommended to use these constants directly.
No Hooks.
Returns
String. The path from the plugin folder to the plugin file without slashes at the ends.
Usage
plugin_basename( $file );
- $file(string) (required)
- Absolute path to the plugin file or folder.
Examples
#1 Demo: get plugin basic name - folder_name/main_file.php
Suppose the plugin file is in this path: /home/www/wp-content/plugins/my-plugin/my-plugin.php
then by calling plugin_basename we get the following:
$plug_basename = plugin_basename( __FILE__ ); // my-plugin/my-plugin.php
Note if plugin consist of one PHP file and lies directly in plugins folder.
For a plugin that consist of just a PHP file (e.g. wp-content/plugin.php) and for a must-use plugin (e.g. wp-content/mu-plugins/plugin.php), plugin_basename( __FILE__ ) returns plugin.php.
#2 What if arbitrary parameters are specified
Usually, the parameter $file specifies the path to the plugin file, but what if strings unrelated to the plugin are specified:
echo plugin_basename( 'foobar' ); // foobar echo plugin_basename( 'foo/bar' ); // foo/bar echo plugin_basename( 'dir/foo.bar' ); // dir/foo.bar echo plugin_basename( '' ); // ''
In this function, for example, $_GET['page'] is passed when defining $plugin_page in the file wp-admin/admin.php.
Notes
- Global. Array.
$wp_plugin_paths
Changelog
| Since 1.5.0 | Introduced. |