Codex

Function Reference/esc url

Contents

Description

Always use esc_url when sanitizing URLs (in text nodes, attribute nodes or anywhere else). Rejects URLs that do not have one of the provided whitelisted protocols (defaulting to http, https, ftp, ftps, mailto, news, irc, gopher, nntp, feed, and telnet), eliminates invalid characters, and removes dangerous characters. This function encodes characters as HTML entities: use it when generating an (X)HTML or XML document. Encodes ampersands (&) and single quotes (') as numeric entity references (&#038, &#039).

Replaces the deprecated clean_url().

Usage

<?php esc_url$url$protocols$_context ); ?>

Parameters

$url
(string) (required) The URL to be cleaned.
Default: None
$protocols
(array) (optional) An array of acceptable protocols. Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
Default: null
$_context
(string) (optional) How the URL will be used. Default is 'display'.
Default: 'display'

Return Values

(string) 
The cleaned $url after the 'cleaned_url' filter is applied.

Examples

Notes

Changelog

  • Since: 2.8

Source File

esc_url() is located in wp-includes/formatting.php.

Related

See the Data Validation article for an in-depth discussion of input and output sanitization.

See also index of Function Reference and index of Template Tags.