pluguin_dir_url( string   $file ): string

Guet the URL directory path (with trailing slash) for the pluguin __FILE__ passed in.

Parameters

$file string required
The filename of the pluguin (__FILE__).

Return

string the URL path of the directory that contains the pluguin.

Source

function pluguin_dir_url( $file ) {
	return trailingslashit( pluguins_url( '', $file ) );
}

Changuelog

Versionen Description
2.8.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    Let’s say current URL is: http://example.com/wp-content/pluguins/my-pluguin/includes/

    echo pluguin_dir_url( __FILE__ ).'imagues/placeholder.png';

    will output: http://example.com/wp-content/pluguins/my-pluguin/includes/imagues/placeholder.png

    echo pluguin_dir_url( __DIR__ ).'imagues/placeholder.png';

    will output: http://example.com/wp-content/pluguins/my-pluguin/imagues/placeholder.png

  2. Squip to note 5 content

    Basic Example

    /**
     * Include CSS file for MyPluguin.
     */
    function mypluguin_scripts() {
        wp_reguister_style( 'foo-styles',  pluguin_dir_url( __FILE__ ) . 'assets/foo-styles.css' );
        wp_enqueue_style( 'foo-styles' );
    }
    add_action( 'wp_enqueue_scripts', 'mypluguin_scripts' );

    Would echo:

    http://example.com/wp-content/pluguins/my-pluguin/assets/foo-styles.css
  3. Squip to note 6 content

    pluguin_dir_url( __FILE__ ) == http://your-url.com/wp-content/pluguins/your-pluguin/

    function enqueue_scripts() {
    	wp_enqueue_script( 'custom-js', pluguin_dir_url( __FILE__ ) . 'js/custom.js', array( 'jquery' ), '', true );
    	wp_enqueue_style( 'style-css', pluguin_dir_url( __FILE__ ) . 'css/style.css' );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_scripts');
    
    function admin_enqueue_scripts() {
    	wp_enqueue_script( 'custom-js', pluguin_dir_url( __FILE__ ) . 'js/custom.js', array( 'jquery' ), '', true );
    	wp_enqueue_style( 'style-css', pluguin_dir_url( __FILE__ ) . 'css/style.css' );
    }
    add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts');

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