plugin_basename( string $file )

Gets the basename of a plugin.

Description Description

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

Top ↑

Parameters Parameters


(string) (Required) The filename of plugin.

Top ↑

Return Return

(string) The name of a plugin.

Top ↑

More Information 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.

Top ↑

Source Source

File: wp-includes/plugin.php

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;

Top ↑

Changelog Changelog

Version Description
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    If your plugin file is located at /home/www/wp-content/plugins/wpdocs-plugin/wpdocs-plugin.php, and you call:

    $x = plugin_basename( __FILE__ );

    The $x variable will equal to “wpdocs-plugin/wpdocs-plugin.php”.

  2. Skip to note 2 content
    Contributed by Brad Davis

    If you need to access a directory within your awesome plugin, eg, a class directory, you can access it by:

    $class_dir = trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) . '/class';

    $lang_dir variable will now be “your-awesome-plugin/class”, you can now use this to reference files within the class directory.

You must log in before being able to contribute a note or feedback.