plugin_basename() WordPress Function

The plugin_basename() function is used to get the base path and name for a plugin. This is useful for plugin developers who need to know where their plugin is located.

plugin_basename( string $file ) #

Gets the basename of a plugin.


Description

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


Top ↑

Parameters

$file

(string)(Required)The filename of plugin.


Top ↑

Return

(string) The name of a plugin.


Top ↑

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

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
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.