plugin_basename

Gets the basename of a plugin.

Description

(string) plugin_basename( (string) $file ); 

This method extracts the name of a plugin from its filename.

Returns (string)

The name of a plugin.

Parameters (1)

0. $file (string)
The filename of plugin.

Usage

  1. if ( !function_exists( 'plugin_basename' ) ) { 
  2. require_once ABSPATH . WPINC . '/plugin.php'; 
  3.  
  4. // The filename of plugin. 
  5. $file = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = plugin_basename($file); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/plugin.php  
  1. function plugin_basename( $file ) { 
  2. global $wp_plugin_paths; 
  3.  
  4. // $wp_plugin_paths contains normalized paths. 
  5. $file = wp_normalize_path( $file ); 
  6.  
  7. arsort( $wp_plugin_paths ); 
  8. foreach ( $wp_plugin_paths as $dir => $realdir ) { 
  9. if ( strpos( $file, $realdir ) === 0 ) { 
  10. $file = $dir . substr( $file, strlen( $realdir ) ); 
  11.  
  12. $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); 
  13. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); 
  14.  
  15. $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#', '', $file); // get relative path from plugins dir 
  16. $file = trim($file, '/'); 
  17. return $file;