plugins_url( string $path = '', string $plugin = '' )

Retrieves a URL within the plugins or mu-plugins directory.


Description Description

Defaults to the plugins directory URL if no arguments are supplied.


Parameters Parameters

$path

(string) (Optional) Extra path appended to the end of the URL, including the relative directory if $plugin is supplied.

Default value: ''

$plugin

(string) (Optional) A full path to a file inside a plugin or mu-plugin. The URL will be relative to its directory. Typically this is done by passing __FILE__ as the argument.

Default value: ''


Top ↑

Return Return

(string) Plugins URL link with optional paths appended.


Top ↑

Source Source

File: wp-includes/link-template.php

3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
function plugins_url( $path = '', $plugin = '' ) {
 
    $path          = wp_normalize_path( $path );
    $plugin        = wp_normalize_path( $plugin );
    $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
 
    if ( ! empty( $plugin ) && 0 === strpos( $plugin, $mu_plugin_dir ) ) {
        $url = WPMU_PLUGIN_URL;
    } else {
        $url = WP_PLUGIN_URL;
    }
 
    $url = set_url_scheme( $url );
 
    if ( ! empty( $plugin ) && is_string( $plugin ) ) {
        $folder = dirname( plugin_basename( $plugin ) );
        if ( '.' != $folder ) {
            $url .= '/' . ltrim( $folder, '/' );
        }
    }
 
    if ( $path && is_string( $path ) ) {
        $url .= '/' . ltrim( $path, '/' );
    }
 
    /**
     * Filters the URL to the plugins directory.
     *
     * @since 2.8.0
     *
     * @param string $url    The complete URL to the plugins directory including scheme and path.
     * @param string $path   Path relative to the URL to the plugins directory. Blank string
     *                       if no path is specified.
     * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
     *                       is specified.
     */
    return apply_filters( 'plugins_url', $url, $path, $plugin );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Common Usage
    The plugins_url() function is commonly used in a plugin file. Passing the __FILE__ PHP magic constant in the place of the $plugin parameter makes the $path relative to the parent directory of that file:

    1
    echo '<img src="' . esc_url( plugins_url( 'images/wordpress.png', __FILE__ ) ) . '" > ';

    The above might output this HTML markup: <img src="http://www.example.com/wp-content/plugins/my-plugin/images/wordpress.png">.

    If you are using the plugins_url() function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP’s dirname() function:

    1
    echo '<img src="' . esc_url( plugins_url( 'images/wordpress.png', dirname(__FILE__) ) ) . '" > ';

    The above might output this HTML markup: <img src="http://www.example.com/wp-content/plugins/images/wordpress.png">.

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