plugin_basename( string $file ): string
- Since
- 1.5.0
- Source
wp-includes/plugin.php:765
Description
This method extracts the name of a plugin from its filename.
Compatibility
- WordPress
- since 1.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filestring- The filename of plugin.
Return value
string- The name of a plugin.
Performance profile
How much work a call to plugin_basename() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 39–40
- Plugin surface
- None
- Called by
- 20
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 48.
Nothing here hands control to plugin code.
20 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost plugin_basename() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 39–40 | wp_normalize_path(), arsort(), wp_normalize_path(), wp_normalize_path(), preg_quote(), preg_quote() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 48 | 39–40 | 3 | |
| 8.5 | 48 | 39–40 | 3 | |
| 8.4 | 48 | 39–40 | 3 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 60 | 45–46 | 3 | |
| 8.2 | 60 | 45–46 | 3 | |
| 8.1 | 60 | 45–46 | 3 | |
| 7.4 | 60 | 45–46 | 3 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 2
- wp_normalize_path()Normalizes a filesystem path.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
Used by · 20
- WP_REST_Plugins_Controller::sanitize_plugin_param()Sanitizes the "plugin" parameter to be a proper plugin file with ".php" appended.
- WP_REST_Plugins_Controller::validate_plugin_param()Checks that the "plugin" parameter is a valid path.
- WP_REST_Templates_Controller::get_wp_templates_author_text_field()Returns a human readable text for the author of the template.
- _get_plugin_data_markup_translate()Sanitizes plugin data, optionally adds markup, optionally translates.
- activate_plugin()Attempts activation of plugin in a "sandbox" and redirects on success.
- add_menu_page()Adds a top-level menu page.
- add_submenu_page()Adds a submenu page.
- deactivate_plugins()Deactivates a single plugin or multiple plugins.
- get_plugin_data()Parses the plugin contents to retrieve plugin's metadata.
- get_plugin_files()Gets a list of a plugin's files.
- get_plugins()Checks the plugins directory and retrieve all plugin files with plugin data.
- is_uninstallable_plugin()Determines whether the plugin can be uninstalled.
Show all 20
- plugins_url()Retrieves a URL within the plugins or mu-plugins directory.
- register_activation_hook()Set the activation hook for a plugin.
- register_deactivation_hook()Sets the deactivation hook for a plugin.
- register_uninstall_hook()Sets the uninstallation hook for a plugin.
- uninstall_plugin()Uninstalls a single plugin.
- wp_ajax_delete_plugin()Handles deleting a plugin via AJAX.
- wp_ajax_update_plugin()Handles updating a plugin via AJAX.
- wp_skip_paused_plugins()Filters a given list of plugins, removing any paused plugins from it.
Source code
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 ( str_starts_with( $file, $realdir ) ) { $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;}Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.