plugin_basename

The timeline below displays how wordpress function plugin_basename has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @since 1.5.0
 *
 * @global array $wp_plugin_paths
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 */
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;
}

WordPress Version: 5.4

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @since 1.5.0
 *
 * @global array $wp_plugin_paths
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 */
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;
}

WordPress Version: 4.6

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @since 1.5.0
 *
 * @global array $wp_plugin_paths
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 */
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);
    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#', '', $file);
    // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
}

WordPress Version: 4.3

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @since 1.5.0
 *
 * @global array $wp_plugin_paths
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 */
function plugin_basename($file)
{
    global $wp_plugin_paths;
    foreach ($wp_plugin_paths as $dir => $realdir) {
        if (strpos($file, $realdir) === 0) {
            $file = $dir . substr($file, strlen($realdir));
        }
    }
    $file = wp_normalize_path($file);
    $plugin_dir = wp_normalize_path(WP_PLUGIN_DIR);
    $mu_plugin_dir = wp_normalize_path(WPMU_PLUGIN_DIR);
    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#', '', $file);
    // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
}

WordPress Version: 4.1

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @since 1.5.0
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 */
function plugin_basename($file)
{
    global $wp_plugin_paths;
    foreach ($wp_plugin_paths as $dir => $realdir) {
        if (strpos($file, $realdir) === 0) {
            $file = $dir . substr($file, strlen($realdir));
        }
    }
    $file = wp_normalize_path($file);
    $plugin_dir = wp_normalize_path(WP_PLUGIN_DIR);
    $mu_plugin_dir = wp_normalize_path(WPMU_PLUGIN_DIR);
    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#', '', $file);
    // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
}

WordPress Version: 4.0

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @since 1.5.0
 *
 * @uses WP_PLUGIN_DIR, WPMU_PLUGIN_DIR
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 */
function plugin_basename($file)
{
    global $wp_plugin_paths;
    foreach ($wp_plugin_paths as $dir => $realdir) {
        if (strpos($file, $realdir) === 0) {
            $file = $dir . substr($file, strlen($realdir));
        }
    }
    $file = wp_normalize_path($file);
    $plugin_dir = wp_normalize_path(WP_PLUGIN_DIR);
    $mu_plugin_dir = wp_normalize_path(WPMU_PLUGIN_DIR);
    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#', '', $file);
    // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
}

WordPress Version: 3.9

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @since 1.5.0
 *
 * @access private
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 * @uses WP_PLUGIN_DIR
 */
function plugin_basename($file)
{
    global $wp_plugin_paths;
    foreach ($wp_plugin_paths as $dir => $realdir) {
        if (strpos($file, $realdir) === 0) {
            $file = $dir . substr($file, strlen($realdir));
        }
    }
    $file = wp_normalize_path($file);
    $plugin_dir = wp_normalize_path(WP_PLUGIN_DIR);
    $mu_plugin_dir = wp_normalize_path(WPMU_PLUGIN_DIR);
    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#', '', $file);
    // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
}

WordPress Version: 3.7

//
// Functions for handling plugins.
//
/**
 * Gets the basename of a plugin.
 *
 * This method extracts the name of a plugin from its filename.
 *
 * @package WordPress
 * @subpackage Plugin
 * @since 1.5
 *
 * @access private
 *
 * @param string $file The filename of plugin.
 * @return string The name of a plugin.
 * @uses WP_PLUGIN_DIR
 */
function plugin_basename($file)
{
    $file = str_replace('\\', '/', $file);
    // sanitize for Win32 installs
    $file = preg_replace('|/+|', '/', $file);
    // remove any duplicate slash
    $plugin_dir = str_replace('\\', '/', WP_PLUGIN_DIR);
    // sanitize for Win32 installs
    $plugin_dir = preg_replace('|/+|', '/', $plugin_dir);
    // remove any duplicate slash
    $mu_plugin_dir = str_replace('\\', '/', WPMU_PLUGIN_DIR);
    // sanitize for Win32 installs
    $mu_plugin_dir = preg_replace('|/+|', '/', $mu_plugin_dir);
    // remove any duplicate slash
    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#', '', $file);
    // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
}