Retrieves the URL to the content directory.
Parameters
$pathstringoptional- 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. Blank string
* if no path is specified.
*/
return apply_filters( 'content_url', $url, $path );
}
Hooks
- apply_filters( ‘content_url’,
string $url ,string $path ) Filters the URL to the content directory.
Changelog
| Version | 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
plugins,themesanduploadsfolders. By default, it is the/wp-contentdirectory. However, users are actually allowed to change the name of this directory and place it anywhere they want, as stated here: https://developer.wordpress.org/plugins/plugin-basics/determining-plugin-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 changing its name requires defining some constants 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 get the WordPress content directory path, you can use the pre-defined
WP_CONTENT_DIRconstant.