wppaste
WordPress

sanitize_url( string $url, string[] $protocols = null ): string

Since
2.3.1, 2.8.0, 5.9.0, 6.9.0
Source
wp-includes/formatting.php:4689

Cleans a URL for storage or redirection by running it through esc_url() with the 'db' context instead of the default display context. Use it when a URL is headed into the database, post meta, an option, or a Location header, not when it's about to be echoed into HTML markup. For markup output, esc_url() itself remains the right call because it entity-encodes ampersands for valid HTML.

Sanitizes a URL for database or redirect usage.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$urlstring
The URL to be cleaned.
$protocolsstring[]optional
An array of acceptable protocols.
Defaults to return value of wp_allowed_protocols().Default: null

Return value

string
The cleaned URL after esc_url() is run with the 'db' context.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Sanitize a URL before saving it as post meta

Post 2 already carries a price meta value, so add a related external link field to it and clean the incoming URL before storing it.

$raw_link = 'http://example.com/product?id=2&ref=<script>alert(1)</script>';
$clean_link = sanitize_url( $raw_link );

update_post_meta( 2, 'external_link', $clean_link );

echo esc_html( 'Stored meta value: ' . $clean_link );

The stripped script tag disappears because esc_url() rejects disallowed characters and protocols before the value ever reaches the database.

Restrict a redirect URL to a limited set of protocols

Build a safe redirect target for a login-like flow where only http and https links should ever be honored.

$requested_url = 'javascript:alert(document.cookie)';
$safe_url = sanitize_url( $requested_url, array( 'http', 'https' ) );

if ( empty( $safe_url ) ) {
	echo esc_html( 'Redirect blocked: no allowed protocol found in the submitted URL.' );
} else {
	echo esc_html( 'Redirecting to: ' . $safe_url );
}

Passing a custom $protocols array replaces the full default list from wp_allowed_protocols(), so it must include every scheme the redirect legitimately needs.

Common problems and fixes · 3

Why does sanitize_url() encode ampersands differently than esc_url()?

sanitize_url() calls esc_url() with the 'db' context, which is meant for values going into storage or a redirect header, not straight into HTML. Because of that, it leaves raw ampersands as '&' instead of converting them to '&' for markup.

Why did my custom protocols list break normal http links?

The $protocols parameter replaces the default whitelist entirely instead of adding to it. If the array passed doesn't include 'http' and 'https', those links get stripped along with anything else not listed.

Why does sanitize_url() return an empty string for a URL that looks fine?

esc_url() (which sanitize_url() wraps) rejects a URL outright when it starts with a scheme not present in the allowed protocols list, or when it contains characters it can't safely clean. There's no partial cleanup in that case, just an empty return value.

Alternatives and related functions

esc_url
When the URL is about to be printed into HTML markup, such as an href or src attribute, and needs entity-encoded ampersands.
esc_url_raw
When working against an older WordPress code reference or a codebase that still calls the pre-6.9 name for this same database-context sanitization.
wp_kses_bad_protocol
When only the protocol portion of a string needs stripping and the rest of esc_url()'s cleanup, like query string handling, isn't wanted.
wp_allowed_protocols
When building a custom $protocols array and the goal is to extend the core defaults rather than replace them outright.

Performance profile

How much work a call to sanitize_url() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8

Executed per call on PHP 8.5. The body compiles to 8.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below sanitize_url()

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_url() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8esc_url()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 8 instructions, 8 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

Used by · 50

Show all 50

Source code

function sanitize_url( $url, $protocols = null ) {	return esc_url( $url, $protocols, 'db' );}

Changelog

Introduced in 2.3.1. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.0
Prepends https:// to the URL if it does not already contain a scheme and the first item in $protocols is 'https'.from the docblock
5.9.0
Restored (un-deprecated).from the docblock
2.8.0
Deprecated in favor of esc_url_raw().from the docblock
2.3.1
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.