content_url()WP 2.6.0

Gets the URL to the content directory (folder wp_content) without a slash (/) at the end. Considers the protocol (https). Takes into account sub-sites if multisite is used.

The function is already available in mu-plugins, but not available in SHORTINIT loading.

Use the constant WP_CONTENT_DIR when you need to get the path to the content folder, not the URL.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.10 sec (speed of light) | PHP 7.3.12, WP 5.4
Hooks from the function

Returns

String.

Usage

content_url( $path );
$path(string)
Additional path to append to the end of the URL. For example: /uploads
Default: ''

Examples

0

#1 Demo: get Content URL

echo content_url(); //> http://www.example.com/wp-content

Take in account, that there is no ending closing slash (/).

Set the path:

echo content_url( '/uploads/' ); //> https://example.com/wp-content/uploads/
echo content_url( 'uploads/' );  //> https://example.com/wp-content/uploads/
echo content_url( 'uploads' );   //> https://example.com/wp-content/uploads

Changelog

Since 2.6.0 Introduced.

content_url() code WP 6.9

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 );
}