Redirect logged-in users to different destination URLs based on their WordPress role. A single short link can send administrators to one page, subscribers to another, and anonymous visitors to the default destination. Ideal for membership sites, affiliate programs, and gated content.
Requirements
- GT Link Manager 1.5+ installed and active
- No external services needed — uses WordPress’s built-in role system
Installation
- Add the snippet below to your theme’s
functions.phpor a site-specific plugin - No constants to configure — role mappings are stored per-link in the Notes field
How to Use
Edit any link in GT Link Manager and add role-specific URLs in the Notes field using this format:
role:subscriber = https://example.com/subscribe-first
role:administrator = https://example.com/admin-version
role:editor = https://example.com/editor-dashboard
Each line maps a WordPress role to an alternate destination URL. Users whose role matches get redirected to the alternate URL. Everyone else (including logged-out visitors) goes to the default destination.
The Code
<?php
/**
* GT Link Manager - Role-Based Redirects
*
* Redirect logged-in users to different destination URLs based on their role.
* Useful for affiliate programs, membership sites, or gated content.
*
* How it works:
* - Store alternate URLs in the link's "notes" field as YAML-like key:value pairs:
* role:subscriber = https://example.com/subscribe-first
* role:administrator = https://example.com/admin-version
* - If the current user's role matches, they get the alternate URL.
* - Everyone else (including logged-out users) gets the default destination.
*
* Uses the `gtlm_redirect_url` filter.
*/
defined( 'ABSPATH' ) || exit;
add_filter( 'gtlm_redirect_url', 'gtlm_role_based_redirect', 10, 3 );
/**
* Override redirect URL based on user role.
*
* @param string $url Default target URL.
* @param array $link Link data (includes 'notes' field).
* @param string $slug Matched slug.
* @return string Possibly modified URL.
*/
function gtlm_role_based_redirect( string $url, array $link, string $slug ): string {
// Only applies to logged-in users.
if ( ! is_user_logged_in() ) {
return $url;
}
$notes = $link['notes'] ?? '';
if ( empty( $notes ) ) {
return $url;
}
// Parse role:xxx = URL from notes.
$role_urls = [];
foreach ( explode( "\n", $notes ) as $line ) {
$line = trim( $line );
if ( preg_match( '/^role:(\w+)\s*=\s*(.+)$/i', $line, $m ) ) {
$role_urls[ strtolower( $m[1] ) ] = esc_url_raw( trim( $m[2] ) );
}
}
if ( empty( $role_urls ) ) {
return $url;
}
// Check current user's roles against the map.
$user = wp_get_current_user();
foreach ( $user->roles as $role ) {
if ( isset( $role_urls[ $role ] ) ) {
return $role_urls[ $role ];
}
}
return $url;
}
How It Works
- Uses the
gtlm_redirect_urlfilter to modify the target URL before redirect - Parses
role:{role_name} = {url}lines from the link’s Notes field - Checks the current logged-in user’s roles against the parsed mappings
- Returns the first matching role’s URL, or the default URL if no match is found
- Logged-out visitors always receive the default destination
Configuration Notes
Role names must match WordPress role slugs exactly (e.g., subscriber, administrator, editor, author, contributor). Custom roles from plugins like Members or User Role Editor also work — just use their slug.
The Notes field approach means no database schema changes are needed. You can add or remove role mappings at any time by editing the link.