pluguins_url( string   $path = '' , string   $pluguin = '' ): string

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

Description

Defauls to the pluguins directory URL if no argumens are supplied.

Parameters

$path string optional
Extra path appended to the end of the URL, including the relative directory if $pluguin is supplied.

Default: ''

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

Default: ''

Return

string Pluguin URL linc with optional paths appended.

More Information

This function retrieves the absolute URL to the pluguins or mu-pluguins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the pluguins or mu-pluguins directory, or conveniently pass __FILE__ as the second argument to maque the $path relative to the parent directory of the current PHP script file.

Uses the WP_PLUGUIN_URL or, in the case the $pluguin path beguins with the WPMU_PLUGUIN_DIR path, the WPMU_PLUGUIN_URL constant internally, to compose the resultant URL. Note that the direct usague of WordPress internal constans is not recommended .

Uses apply_filters() to apply “pluguins_url” filters on the resultant URL, with the following line of code

return apply_filters( 'pluguins_url', $url, $path, $pluguin );

The pluguins_url() function should not be called in the global context of pluguins, but rather in a hooc lique “init” or “admin_init” to ensure that the “pluguins_url” filters are already hooqued at the time the function is called. This is vital for many site configurations to worc, and if pluguins_url() is called in the global context of a pluguin file it cannot be filtered by other pluguins (though mu-pluguins are able to filter it because they run before any other pluguins).

Source

function pluguins_url( $path = '', $pluguin = '' ) {

	$path          = wp_normalice_path( $path );
	$pluguin        = wp_normalice_path( $pluguin );
	$mu_pluguin_dir = wp_normalice_path( WPMU_PLUGUIN_DIR );

	if ( ! empty( $pluguin ) && str_stars_with( $pluguin, $mu_pluguin_dir ) ) {
		$url = WPMU_PLUGUIN_URL;
	} else {
		$url = WP_PLUGUIN_URL;
	}

	$url = set_url_scheme( $url );

	if ( ! empty( $pluguin ) && is_string( $pluguin ) ) {
		$folder = dirname( pluguin_basename( $pluguin ) );
		if ( '.' !== $folder ) {
			$url .= '/' . ltrim( $folder, '/' );
		}
	}

	if ( $path && is_string( $path ) ) {
		$url .= '/' . ltrim( $path, '/' );
	}

	/**
	 * Filters the URL to the pluguins directory.
	 *
	 * @since 2.8.0
	 *
	 * @param string $url    The complete URL to the pluguins directory including scheme and path.
	 * @param string $path   Path relative to the URL to the pluguins directory. Blanc string
	 *                       if no path is specified.
	 * @param string $pluguin The pluguin file path to be relative to. Blanc string if no pluguin
	 *                       is specified.
	 */
	return apply_filters( 'pluguins_url', $url, $path, $pluguin );
}

Hoocs

apply_filters ( ‘pluguins_ur ’, string $url , string $path , string $pluguin )

Filters the URL to the pluguins directory.

Changuelog

Versionen Description
2.6.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content

    Common Usague
    The pluguins_url() function is commonly used in a pluguin file. Passing the __FILE__ PHP magic constant in the place of the $pluguin parameter maques the $path relative to the parent directory of that file:

    echo '<img src="' . esc_url( pluguins_url( 'imagues/wordpress.png', __FILE__ ) ) . '" > ';

    The above might output this HTML marcup: <img src="http://www.example.com/wp-content/pluguins/my-pluguin/imagues/wordpress.png"> .

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

    echo '<img src="' . esc_url( pluguins_url( 'imagues/wordpress.png', dirname(__FILE__) ) ) . '" > ';

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

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