Redirect a WordPress page or post to an external URL using a custom field, enabling marketing redirects, affiliate links, and campaign tracking URLs managed from the admin without touching code.
// Add a meta box to set the redirect URL
add_action( 'add_meta_boxes', function() {
add_meta_box(
'nahnu_redirect_url',
'Redirect to URL',
function( $post ) {
$url = get_post_meta( $post->ID, '_redirect_url', true );
wp_nonce_field( 'nahnu_redirect_save', 'nahnu_redirect_nonce' );
echo '<p style="color:#64748b;font-size:12px;margin-top:0">If set, visitors will be redirected to this URL instead of seeing this page. Leave empty to show the page normally.</p>';
echo '<input type="url" name="nahnu_redirect_url" value="' . esc_attr( $url ) . '" style="width:100%" placeholder="https://example.com/destination">';
},
[ 'post', 'page' ],
'side',
'default'
);
} );
// Save the redirect URL
add_action( 'save_post', function( $post_id ) {
if ( ! isset( $_POST['nahnu_redirect_nonce'] ) ) return;
if ( ! wp_verify_nonce( $_POST['nahnu_redirect_nonce'], 'nahnu_redirect_save' ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
$url = esc_url_raw( $_POST['nahnu_redirect_url'] ?? '' );
if ( $url ) {
update_post_meta( $post_id, '_redirect_url', $url );
} else {
delete_post_meta( $post_id, '_redirect_url' );
}
} );
// Perform the redirect
add_action( 'template_redirect', function() {
if ( ! is_singular() ) return;
$url = get_post_meta( get_the_ID(), '_redirect_url', true );
if ( $url ) {
wp_redirect( $url, 301 );
exit;
}
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Marketing campaigns, affiliate programmes, and partnership links often need their own clean, branded URLs, for example yourdomain.com/go/partner-name redirecting to an affiliate link with tracking parameters. Managing these as WordPress pages with a redirect field lets non-technical team members create, update, and retire redirect links from the admin without code deployments. This snippet provides a complete implementation: a sidebar meta box for the redirect URL, a save handler, and the redirect itself.
The snippet adds a “Redirect to URL” field to the sidebar of posts and pages. Enter a URL and save, visitors to that page will be redirected. Clear the field and save to restore normal page behaviour.
The meta box uses the side position to place it in the document settings sidebar rather than below the content editor. The instructions in the meta box make the behaviour clear to non-technical users, if the field is empty, the page works normally; if it has a URL, visitors are redirected. type="url" on the input provides browser-level URL validation on modern browsers.
esc_url_raw() sanitises the URL before saving, it strips unsafe characters but preserves query strings and fragments, which is appropriate for redirect destinations that may include tracking parameters like ?utm_source=partner&utm_medium=referral. When the field is cleared, the meta key is deleted rather than saved as an empty string, which keeps the database clean.
The redirect fires on template_redirect using a 301 status. Use 301 for permanent redirects, affiliate links, evergreen partnership pages, long-term campaign URLs. Use 302 for temporary redirects by changing the status code, seasonal campaigns, A/B tests, or redirects you expect to remove.
Common applications include affiliate and partner link management (/go/toolname redirecting to an affiliate URL), campaign tracking URLs that redirect to landing pages with UTM parameters, link shortening for social media (/s/article-slug redirecting to the full URL), and sunset page redirects where old pages should redirect to their replacement permanently. All of these become manageable by marketing teams through the admin without developer involvement.