On this page
plugin_basename( string $file ): string
Gets the basename of a plugin.
Description
This method extracts the name of a plugin from its filename.
Parameters
$filestring Required-
The filename of plugin.
Return
string The name of a plugin.
More Information
This function gets the path to a plugin file or directory, relative to the plugins directory, without the leading and trailing slashes.
Uses both the WP_PLUGIN_DIR and WPMU_PLUGIN_DIR constants internally, to test for and strip the plugins directory path from the $file path. Note that the direct usage of WordPress internal constants is not recommended.
Source
File: wp-includes/plugin.php. View all references
function plugin_basename( $file ) {
global $wp_plugin_paths;
// $wp_plugin_paths contains normalized paths.
$file = wp_normalize_path( $file );
arsort( $wp_plugin_paths );
foreach ( $wp_plugin_paths as $dir => $realdir ) {
if ( strpos( $file, $realdir ) === 0 ) {
$file = $dir . substr( $file, strlen( $realdir ) );
}
}
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
// Get relative path from plugins directory.
$file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file );
$file = trim( $file, '/' );
return $file;
}
Related
Uses
| Uses | Description |
|---|---|
| wp_normalize_path() wp-includes/functions.php | Normalizes a filesystem path. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Plugins_Controller::validate_plugin_param() wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php | Checks that the “plugin” parameter is a valid path. |
| WP_REST_Plugins_Controller::sanitize_plugin_param() wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php | Sanitizes the “plugin” parameter to be a proper plugin file with “.php” appended. |
| wp_skip_paused_plugins() wp-includes/load.php | Filters a given list of plugins, removing any paused plugins from it. |
| wp_ajax_delete_plugin() wp-admin/includes/ajax-actions.php | Ajax handler for deleting a plugin. |
| wp_ajax_update_plugin() wp-admin/includes/ajax-actions.php | Ajax handler for updating a plugin. |
| is_uninstallable_plugin() wp-admin/includes/plugin.php | Determines whether the plugin can be uninstalled. |
| uninstall_plugin() wp-admin/includes/plugin.php | Uninstalls a single plugin. |
| add_menu_page() wp-admin/includes/plugin.php | Adds a top-level menu page. |
| add_submenu_page() wp-admin/includes/plugin.php | Adds a submenu page. |
| deactivate_plugins() wp-admin/includes/plugin.php | Deactivates a single plugin or multiple plugins. |
| activate_plugin() wp-admin/includes/plugin.php | Attempts activation of plugin in a “sandbox” and redirects on success. |
| get_plugin_data() wp-admin/includes/plugin.php | Parses the plugin contents to retrieve plugin’s metadata. |
| _get_plugin_data_markup_translate() wp-admin/includes/plugin.php | Sanitizes plugin data, optionally adds markup, optionally translates. |
| get_plugin_files() wp-admin/includes/plugin.php | Gets a list of a plugin’s files. |
| get_plugins() wp-admin/includes/plugin.php | Checks the plugins directory and retrieve all plugin files with plugin data. |
| plugins_url() wp-includes/link-template.php | Retrieves a URL within the plugins or mu-plugins directory. |
| register_activation_hook() wp-includes/plugin.php | Set the activation hook for a plugin. |
| register_deactivation_hook() wp-includes/plugin.php | Sets the deactivation hook for a plugin. |
| register_uninstall_hook() wp-includes/plugin.php | Sets the uninstallation hook for a plugin. |
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/plugin_basename