Role-Based Redirects

  • JNext lesson
  • KPrevious lesson
  • FSearch lessons
  • EscClear search

Role based redirects in WordPress send one short link to different destinations depending on who’s logged in: members to the content, admins to the edit screen, everyone else to the sales page. This snippet implements role resolution on GT Link Manager’s gtlm_redirect_url filter, ideal for membership sites and client portals.

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.php or 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_url filter 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.

Role checks run after WordPress authentication, which the redirect system lesson explains in its resolution pipeline. Combine with custom headers to keep these redirects out of shared caches.