If current logged in user matches user ID of an entry

By: Will J | Asked: 07/08/2023
ForumsCategory: General questionsIf current logged in user matches user ID of an entry
Will J asked 9 months ago

Hello! This may not be staying within the boundaries of strictly FF, but I'm trying to get certain div's to display only if the current logged in user matches the user ID of a certain entry. I can get the user ID of the entry, but [current_user] isn't working to match it up. I think I even know the function to make it work but can't figure out how to get the current logged in user in order to set it as a variable. Thanks for any advise in advance!

Victor Font replied 9 months ago

How and where are you comparing user id and current user?

Asking for A Friend replied 9 months ago

[if 161 equals=[current_user][formidable id="24"][/if 161]
In the view, using original form as the data source to pull the user_ID (161) to get form 24 to display when the user_ID of the submitted entry from the original form matches the current logged in user.
Not working...=(

Asking for A Friend replied 9 months ago

Also, following the rules from https://formidableforms.com/knowledgebase/conditionals/

[if 161 equals="current"][formidable id="24"][/if 161]

doesn't work either. So, what's the best way to limit views (or even forms if that'll work) so that the only person who can see it would be the user ID of another form if they are the current user?

Victor Font replied 9 months ago

if you're certain the values match and that code is not working, then I suggest you open a ticket with S11. This requires hands-on trouble shooting.

Asking for A Friend replied 9 months ago

Thanks Victor - what is the short code to show the current logged in user? I've tried a bunch - [current], [current_user], [current_ID] - and none return with the user name (in this case mine) that's logged in. They all just show the code on the page.

Rob LeVineRob LeVine replied 9 months ago

I don't think there is one out of the box. You can write a custom shortcode and have its code return get_current_user_id(). https://connect.formidableforms.com/user-tutorials/custom-shortcode-creation-basic/

Victor Font replied 9 months ago

User id fields usually display the name. You can display any value from the WordPress user record by including the show=name parameter;

Asking for A Friend replied 9 months ago

Thank you both for taking the time to respond! While I try to work out what you said...trust me, it'll take a while... I thought maybe if I explained the goal, it might help come up with a solution that doesn't involve coding... as coding is scary.

So, Bob fills out form 1. His name is listed in the hidden field under user ID for that entry. Bob then goes on about his day and later comes back to the website where he can click on his entry to see if there is any activity. Bob should see form 2 on the side that allows him to control changes to the page that holds his entry. (Those parts, surprisingly, I've figured out. My problem is...) Bob sees form 2 on the side, but so does John when visiting Bob's page. John should not see that form as it's not his entry. Only Bob. So... I figured the way to work that out would be to say... if user ID in the hidden field for that entry = Bob and the current logged in user on that page = Bob then show form 2. If user ID in the hidden field = Bob and current logged in user on that page = John then show form 3.

I'm sure there's a simple, easy way to do it that probably doesn't even involve that comparison, but I'm not finding it. I dabbled in user roles/permissions, but that route didn't pan out as of yet. So, any help would be greatly appreciated!! =) Thanks again!

Victor Font replied 9 months ago

If you check the database, the user_id field that can be added to forms stores the user_id not the name. When Formidable displays the field in a view, it displays the name, not the id. It gets the name from the linked WordPress user record.

The [current_user] shortcode apparently does not work in a view conditional statement. I tested it and it only displays the shortcode itself, the shortcode's output. This could be a bug, but I don't know what the S11 developers intended. That's a question for S11 to answer.

To solve this problem, I would use my own shortcode to return the user id. It would look something like this in practice:

[if 161 show="ID" equals=[my_userid_shortcode] ][formidable id="24"][/if 161]

To create the shortcode, use:

function my_userid_shortcode_callback( $atts ) {
return get_current_user_id();

Asking for A Friend replied 9 months ago

Me again... sorry to be a pain, but it's still giving me grief! I was able to work the short code above (thank you!) and (...after learning what a shortcode was...) also found one to get the current user email so it wouldn't run into the name/number issue with the user ID. The shortcodes for both 194 & current_user_email show up correctly as the email on the page independently.

Below is my failed attempt. It's not blocking either form.

[if 194 equals="[current_user_email]"][formidable id="24"][/if 194]
[if 194 not_equal="[current_user_email]"][formidable id="32"][/if 194]

Can I do a javascript function or is that combo with the php shortcode a no go? Any other thoughts?? Or am I simply barking up the wrong tree here? Seems like verifying the logged in user against something else is something a lot of sites would need to do.

Thanks in advance for the continued help!

PS - I'd also escalated the [current_user] issue by opening a ticket, but not sure if that's the same as S11?

Victor Font replied 9 months ago

S11 = Strategy 11, the Formidable Developers. Without looking at your form, I can't help you any further. If you need a developer's help, please see https://formidable-masterminds.com/developers-directory/

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