BrightLeaf Digital Code Snippet Library
Check out our growing library of code snippets for Gravity Forms!
Create "user role" merge tag
This code snippet creates a custom {user_role} merge tag for Gravity Forms. It dynamically retrieves the current user's WordPress role(s) and allows you to use this information within form notifications, confirmations, and field values.
Features
- Adds two merge tags: {user_role} (all roles) and {user_primary_role} (primary role).
- Automatically retrieves the logged-in user's role(s) and replaces the merge tag with the corresponding value(s).
- Supports multiple roles, displaying them (by default) as a comma-separated list.
- Enhances form personalization by dynamically adapting form content based on user roles.
- Easily customizable separator for multiple roles.
- Optional modifiers:
:displayfor role display names,:separator()to customize the delimiter. - Configurable defaults for separator, logged-out label, and display preference.
- Graceful handling for logged-out users via a configurable label.
- Usable in notifications, confirmations, field labels, and default values.
USAGE:
Available tags:
{user_role}— outputs all roles for the current user joined by the separator (default: ", ").{user_role:display}— same as above but uses role display names.{user_role:separator(|)}— uses "|" to join multiple roles.{user_role:display:separator( | )}— combine modifiers; order does not matter.{user_primary_role}— outputs the user's primary role (first role).{user_primary_role:display}— primary role's display name.
Modifiers:
:display— outputs display names instead of slugs. Works with both tags.:separator(<string>)— sets the delimiter for joining roles when using{user_role}. Ignored for{user_primary_role}. Default is ", ".- The separator is taken literally, can include spaces, and must not contain a closing parenthesis.
Behavior:
- If the user is not logged in or has no roles, outputs the configured
logged_out_label(default: empty string).
For users with multiple roles, the primary role is the first in their roles array.
CONFIGURATION:
- Adjust the defaults in the $cfg block below if desired.
separator: string used to join roles for{user_role}.logged_out_label: what to output for logged-out users (empty string by default).prefer_display_names: whether to default to display names instead of slugs (can be overridden per-tag using:display).
Install Using Code Snippets
This code is best installed directly on your site using the Code Snippets plugin.
Check Out the Code
Loading...
Stay Updated On This Snippet
Review this entry
You must be logged in to post a comment.