plugin_basename()

 

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 );

	$file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file ); // get relative path from plugins dir
	$file = trim( $file, '/' );
	return $file;
}

参数:plugin_basename( string $file )

此方法从插件的文件名中提取插件的名称。

The filename of plugin.

(字符串)插件的名称。

$file

(string)
(Required)
The filename of plugin.

{acontent3}

  1. 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. 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.

10.00% (2) 次Baidu访问
5.00% (1) 次Google访问
80.00% (16) 次Sogou访问
5.00% (1) 次sm访问
  • 蜘蛛:Baidu,2次,更新时间:2019-05-18 11:35:16
  • 蜘蛛:Google,1次,更新时间:2019-05-10 08:29:03
  • 蜘蛛:Sogou,16次,更新时间:2019-05-26 00:11:31
  • 蜘蛛:sm,1次,更新时间:2019-05-26 03:31:32

参与评论