Independent Editorial DeskWordPress Releases, Builds, and Operations
Back to Archive
Hardening NotesImplementation Notes

WordPress wp_safe_redirect Example: Prevent Open Redirects

Use wp_safe_redirect and wp_validate_redirect to prevent open redirects, validate return URLs, choose status codes, and exit safely after redirects.

Published

April 22, 2026

Reading Time

2 min read

Updated

April 22, 2026

Abstract safe redirect path protected by a lock to prevent open redirects.
Control LedgerHardening Notes

Hardening Notes

Baselines, access reduction, and default settings that stand up in production.

Best For

Teams preparing, launching, or maintaining WordPress as a backend service in a production stack.

Primary Topics

Hardening NotesImplementation Notes

Editorial Focus

Control Ledger: Baselines, access reduction, and default settings that stand up in production. Updated on April 22, 2026.

Full Report

Last reviewed: April 22, 2026

Redirect code looks harmless until a plugin accepts a user-controlled destination and creates an open redirect. Attackers use open redirects to make phishing links look trusted because the URL starts on a legitimate domain.

This guide shows how to use wp_safe_redirect() for local redirects, how to validate a return URL, and why redirect code should almost always call exit immediately after sending headers.

Problem this prevents

A redirect parameter such as ?redirect_to=https://attacker.example should not send visitors away from the site unless that behavior is explicitly intended and safely allowlisted. WordPress provides wp_safe_redirect() to restrict redirects to allowed hosts.

Safe admin redirect after save

<?php
function vulnwp_redirect_after_settings_save() {
	$url = add_query_arg(
		array(
			'page'    => 'vulnwp-settings',
			'updated' => '1',
		),
		admin_url( 'options-general.php' )
	);

	wp_safe_redirect( $url );
	exit;
}

The redirect target is built from trusted WordPress functions and local query arguments. The exit prevents later code from continuing after redirect headers are sent.

Validate a return URL

function vulnwp_get_safe_return_url() {
	$fallback = admin_url( 'index.php' );
	$raw      = isset( $_GET['redirect_to'] )
		? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) )
		: '';

	if ( '' === $raw ) {
		return $fallback;
	}

	return wp_validate_redirect( $raw, $fallback );
}

wp_validate_redirect() returns the provided URL only if it is safe. Otherwise it returns the fallback.

Use the safe URL

$return_url = vulnwp_get_safe_return_url();

if ( wp_safe_redirect( $return_url, 302, 'VulnWP Plugin' ) ) {
	exit;
}

The third parameter sets the X-Redirect-By header so operators can identify which code initiated the redirect.

Status code choice

Use 302 for temporary redirects after form submissions, login flows, or admin actions. Use 301 only when the redirect is permanent and cache-safe. Browsers and intermediaries can cache permanent redirects aggressively, so a wrong 301 can be painful to undo.

Testing workflow

  1. Test a normal local return URL.
  2. Test an external attacker-style URL and confirm it falls back safely.
  3. Confirm the redirect happens before any output is sent.
  4. Confirm code exits after redirect.
  5. Check the X-Redirect-By header during debugging.

When external redirects are legitimate

Some integrations need external redirects, such as OAuth, payment providers, or single sign-on systems. In those cases, use a strict allowlist of provider hosts and avoid accepting arbitrary destination URLs from request parameters.

$allowed_hosts = array( 'accounts.example.com', 'checkout.example.com' );
$host          = wp_parse_url( $return_url, PHP_URL_HOST );

if ( ! in_array( $host, $allowed_hosts, true ) ) {
	$return_url = home_url( '/' );
}

Production checklist

  • Prefer wp_safe_redirect() for local redirects.
  • Always call exit after successful redirects.
  • Use trusted fallbacks.
  • Validate user-provided return URLs.
  • Allowlist external hosts only when required.
  • Do not redirect based on unsanitized request values.

Common mistakes

  • Using wp_redirect() with request data. That can create open redirects.
  • No fallback. Invalid return URLs need a safe destination.
  • Forgetting exit. Code can continue executing after headers are sent.
  • Allowlisting too broadly. Use exact host rules, not loose string matching.
  • Redirecting after output. Headers must be sent before body output.

Related reading

For URL routing, read the rewrite rules example. For admin action protection, pair this with the nonce example.

References and further reading

Popular Guides

Popular WordPress guides to read next.

These articles connect recurring production concerns: implementation details, updates, troubleshooting, recovery paths, and operational cleanup.

Continue Reading

More from the archive.

Diagnostic dashboard scene representing a WordPress Site Health review before major updates.
01Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Site Health Check Before Major Updates: What to Review First

A pre-update WordPress Site Health checklist covering loopbacks, connectivity, debug settings, and environment readiness.

Structured data and route review scene representing permalink validation after a WordPress migration.
02Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Permalink Checklist After Migration: Catch URL Problems Early

A post-migration WordPress permalink checklist for checking rewrite rules, post URLs, archives, and redirect noise.

Technical media workspace representing image preparation and optimization before upload to WordPress.
03Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Image Optimization Checklist: What to Fix Before Upload

A practical WordPress image optimization checklist covering dimensions, compression, formats, and Media settings before upload.