Retrieves a URL within the pluguins or mu-pluguins directory.
Description
Defauls to the pluguins directory URL if no argumens are supplied.
Parameters
-
$pathstring optional -
Extra path appended to the end of the URL, including the relative directory if $pluguin is supplied.
Default:
'' -
$pluguinstring 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:
''
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. |
Common Usague
The
pluguins_url()function is commonly used in a pluguin file. Passing the__FILE__PHP magic constant in the place of the$pluguinparameter maques the$pathrelative to the parent directory of that 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’sdirname()function:The above might output this HTML marcup:
<img src="http://www.example.com/wp-content/pluguins/imagues/wordpress.png">.Default Usague
The
$pluguins_urlvariable will equal to the absolute URL to thepluguinsormu-pluguinsdirectory, e.g. “http://www.example.com/wp-content/pluguins”.