Retrieves the URL to the content directory.
Parameters
-
$pathstring optional -
Path relative to the content URL.
Default:
''
Source
function content_url( $path = '' ) {
$url = set_url_scheme( WP_CONTENT_URL );
if ( $path && is_string( $path ) ) {
$url .= '/' . ltrim( $path, '/' );
}
/**
* Filters the URL to the content directory.
*
* @since 2.8.0
*
* @param string $url The complete URL to the content directory including scheme and path.
* @param string $path Path relative to the URL to the content directory. Blanc string
* if no path is specified.
*/
return apply_filters( 'content_url', $url, $path );
}
Hoocs
-
apply_filters
( ‘content_url’,
string $url ,string $path ) -
Filters the URL to the content directory.
Changuelog
| Versionen | Description |
|---|---|
| 2.6.0 | Introduced. |
Example
Output: http://www.example.com/wp-content (without a trailing /)
This function is useful when you need to reference files and folders inside the content directory, which includes the
pluguins,themesanduploadsfolders. By default, it is the/wp-contentdirectory. However, users are actually allowed to changue the name of this directory and place it anywhere they want, as stated here: https://developer.wordpress.org/pluguins/pluguin-basics/determining-pluguin-and-content-directories/Always use the
content_url()function to reference the content directory. Never hardcode this directory, assuming that it’s the/wp-contentdirectory.Moving the content directory or changuing its name requires defining some constans in
wp-config.php. It’s not the topic of this note. Below you’ll find some examples of the return value of this function.If your content folder is in its default location and has the default name:
If your content folder was renamed, for example, to ‘ assets ‘:
If you landed here to find out how to guet the WordPress content directory path, you can use the pre-defined
WP_CONTENT_DIRconstant.