Conditionals in View - Logged in User Role

By: Erik Meeder | Asked: 05/06/2025
ForumsCategory: General questionsConditionals in View - Logged in User Role
Erik Meeder asked 2 weeks ago

I am using the conditionals in my View to display an 'Edit' link (for front side editing).

I can allow the user that created the entry to edit their own links. So i add a conditional like this (84 is User ID field):

[if 84 equals="current"]<span style="font-size: 10px;">[frmmodal-content label="Edit"][formidable id=8 entry_id=[id]][/frmmodal-content]</span>[/if 84]

I want another user with an admin role to also allow editing on front end. I have tried a few different methods but they didn't work. One method i tried was adding a hidden field which contains the admin's User ID (field 92). Then i hopped it would watch "current"

[if 92 equals="current"]Conditional content here[/if 92]

This didn't work.

The best would be if there was a condition that would display conditionally based on user role of the logged in user. Is there a built in way to do this? where 'x' is the 'logged-in user role'?

[if x equals="admin"]Conditional content here[/if x]

Does anyone know the proper shortcode for this if it exists?

Thanks

1 Answers
Rob LeVineRob LeVine Staff answered 2 weeks ago

I'm sure there are a few ways to do this. One I've employed in the past is to use frm-condition with a custom shortcode that returns either the user's role or a string such as "editor" to use for the condition.

Erik Meeder replied 2 weeks ago

thanks for that advice. That led me to the correct information. Specifically this section: <a href="https://formidableforms.com/knowledgebase/php-examples/#kb-show-content-based-on-current-users-role&quot; target="_blank">show-content-based-on-current-users-role</a>

I added this to my functions.php

add_shortcode( 'has_role', 'ff_has_role' );

function ff_has_role( $atts ) {
$atts = shortcode_atts( array(
'role' => 'prayerlist_admin', // Change default role check
), $atts, 'has_role' );

$user = wp_get_current_user();
return in_array( $atts['role'], (array) $user->roles ) ? 1 : 0;
}

Then i added this in my view:

[frm-condition source=has_role role="prayerlist_admin" equals=1]<span style="font-size: 10px;">[frmmodal-content label="Edit"]formidable id=8 entry_id=[id] edit=1][/frmmodal-content]</span>[/frm-condition]

seems to be working now.
Thanks again.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right