函数文档

plugin_basename()

💡 云策文档标注

概述

plugin_basename() 函数用于获取插件相对于插件目录的基名,去除前导和尾随斜杠。它内部使用 WP_PLUGIN_DIR 和 WPMU_PLUGIN_DIR 常量来提取路径,但直接使用这些常量不推荐。

关键要点

  • 函数功能:从插件文件名中提取插件基名,返回相对于插件目录的路径。
  • 参数:$file(字符串,必需),插件文件名。
  • 返回值:字符串,插件基名。
  • 内部处理:使用 wp_normalize_path() 规范化路径,并处理 $wp_plugin_paths 全局变量以支持路径映射。
  • 注意事项:直接使用 WordPress 内部常量(如 WP_PLUGIN_DIR)不推荐,应通过此函数间接使用。

代码示例

// 示例:获取当前插件文件的基名
$x = plugin_basename( __FILE__ );
// 如果插件文件位于 /wp-content/plugins/wpdocs-plugin/wpdocs-plugin.php
// $x 将等于 "wpdocs-plugin/wpdocs-plugin.php"

注意事项

  • 对于单文件插件或 must-use 插件,plugin_basename( __FILE__ ) 可能返回与 basename( __FILE__ ) 相同的值,但在子目录中不同。
  • 建议定义常量(如 WPDOCS_PLUGIN_BASE)来存储 plugin_basename( __FILE__ ) 的结果,以便在插件其他部分重用。

📄 原文内容

Gets the basename of a plugin.

Description

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

Parameters

$filestringrequired
The filename of plugin.

Return

string The name of a plugin.

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.

Source

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

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    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 5 content

    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.

  3. Skip to note 6 content

    If you want to add a plugin action link but need to use the callback action from another file or class than you can try this way

    if ( ! defined( 'WPDOCS_PLUGIN_BASE' ) ) {
            // in main plugin file 
            define( 'WPDOCS_PLUGIN_BASE', plugin_basename( __FILE__ ) );
    }

    And now in other file you can easily use this contrast

    add_filter( 'plugin_action_links_' . WPDOCS_PLUGIN_BASE, 'wpdocs_plugin_settings_link' );
    
    function wpdocs_plugin_settings_link( $links ) { 
            $row_meta = array(
            	'settings' => '<a href="' . esc_attr( get_admin_url( null, 'admin.php?page=wpdocs-settings' ) . '">' . __( 'Settings' ) . '</a>',
            );
            
            return array_merge( $links, $row_meta );
    }

    so it is good practice to define a CONSTANT for plugin_basename( __FILE__ ) and reuse it again.