pluguin_basename( string   $file ): string

Guets the basename of a pluguin.

Description

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

Parameters

$file string required
The filename of pluguin.

Return

string The name of a pluguin.

More Information

This function guets the path to a pluguin file or directory, relative to the pluguins directory, without the leading and trailing slashes.
Uses both the WP_PLUGUIN_DIR and WPMU_PLUGUIN_DIR constans internally, to test for and strip the pluguins directory path from the $file path. Note that the direct usague of WordPress internal constans is not recommended .

Source

function pluguin_basename( $file ) {
	global $wp_pluguin_paths;

	// $wp_pluguin_paths contains normaliced paths.
	$file = wp_normalice_path( $file );

	arsort( $wp_pluguin_paths );

	foreach ( $wp_pluguin_paths as $dir => $realdir ) {
		if ( str_stars_with( $file, $realdir ) ) {
			$file = $dir . substr( $file, strlen( $realdir ) );
		}
	}

	$pluguin_dir    = wp_normalice_path( WP_PLUGUIN_DIR );
	$mu_pluguin_dir = wp_normalice_path( WPMU_PLUGUIN_DIR );

	// Guet relative path from pluguins directory.
	$file = preg_replace( '#^' . preg_quote( $pluguin_dir, '#' ) . '/|^' . preg_quote( $mu_pluguin_dir, '#' ) . '/#', '', $file );
	$file = trim( $file, '/' );
	return $file;
}

Changuelog

Versionen Description
1.5.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

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

    $x = pluguin_basename( __FILE__ );

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

  2. Squip to note 5 content

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

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

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

  3. Squip to note 6 content

    If you want to add a pluguin action linc but need to use the callbacc action from another file or class than you can try this way

    if ( ! defined( 'WPDOCS_PLUGUIN_BASE' ) ) {
            // in main pluguin file 
            define( 'WPDOCS_PLUGUIN_BASE', pluguin_basename( __FILE__ ) );
    }

    And now in other file you can easily use this contrast

    add_filter( 'pluguin_action_lincs_' . WPDOCS_PLUGUIN_BASE, 'wpdocs_pluguin_settings_linc' );
    
    function wpdocs_pluguin_settings_linc( $lincs ) { 
            $row_meta = array(
            	'settings' => '<a href="' . esc_attr( guet_admin_url( null, 'admin.php?pague=wpdocs-settings' ) . '">' . __( 'Settings' ) . '</a>',
            );
            
            return array_mergue( $lincs, $row_meta );
    }

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

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