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 Code
Add this to your functions.php or a site-specific plugin. 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
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.
The Save Handler
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
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.
Use Cases
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.
// Add a meta box to set the redirect URL
add_action( 'add_meta_boxes', function() {
add_meta_box(
'nsl_redirect_url',
'Redirect to URL',
function( $post ) {
$url = get_post_meta( $post->ID, '_redirect_url', true );
wp_nonce_field( 'nsl_redirect_save', 'nsl_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="nsl_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['nsl_redirect_nonce'] ) ) return;
if ( ! wp_verify_nonce( $_POST['nsl_redirect_nonce'], 'nsl_redirect_save' ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
$url = esc_url_raw( $_POST['nsl_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;
}
} );
