How do I get the entry ID of the current user's registration form?

By: Simon Gledhill | Asked: 09/23/2022
ForumsCategory: How-toHow do I get the entry ID of the current user's registration form?
Simon Gledhill asked 2 years ago
I have a form called "Module Form" which is used to record modules for training courses. Each module has a module leader. The module leader field is a dynamic fields which pulls data from my User Registration Form. Therefore, this dynamic field contains the module leader's entry ID number from the user registration form. I want to conditionally display content in a view, if the current user is the module leader, using the dynamic module leader field. I am able to call the module leader value and display the entry ID of the module leader's user registration entry.  What I cannot do, and need help with please, is this: I cannot work out how to get the entry ID of the current_user from the registration form. I can only get the current_user's user ID. But I want entry ID because that is what the Module Leader field is pulling.     Many thanks!
1 Answers
Rob LeVineRob LeVine answered 2 years ago
I'm not clear on when you want to get the information, so I'll just give you the PHP way.  You can create a shortcode that refers to a PHP program that can issue a SQL command or similar to get the information you want from the information you have.  Let me know if you need more details.
Simon Gledhill replied 2 years ago

Thanks for the response. I'm capable of adding a pre-made php snippet and using a custom shortcode... but that's my technical ability limit unfortunately.

I'm attempting to do all of this within a view. The idea is that IF the current user is the module leader then they will see a delete link.

I just can't get the conditional logic to work. I can get the name of the module leader easily, and I can get the name of the current user easily. But the problem is that the name of the module leader is being driven by entry ID where as current user is driven by user ID. So although the two values look the page, they are in fact differnet things that I cannot compare in a conditional statement.

Rob LeVineRob LeVine replied 2 years ago

What you want to use is a variation on https://formidableforms.com/knowledgebase/php-examples/#kb-show-content-based-on-current-users-role

The remaining question is what is the logic in the PHP program that figures out if the user attached to the entry id is the module leader. When you say "current user", do you mean that the view is only displaying one entry at a time, i.e., the user logs in and sees his/her own data vs. a user logs in and sees a table of entries for all sorts of users?

Simon Gledhill replied 2 years ago

So I'm actually already using that snippet to make a delete link appear if the user has a 'Lead Subscriber' role. I have a view with many modules (training course titles) appearing in a list, powered by the module form. And then I have a view which displays which resources are linked to that module (powered by a module links form).

To be clear, module leader is not a user role. The Module form has a field for who the module leader is. This is a dynamic field pulling from the registration form. So all I need to do is check if that field contains the same user as the current_user. But my issue is that the dynamic field pulls entry ID whereas Current_user pulls user_id. Therefore even though both results display the same name, I cannot check that the two are equal.

Thanks for your help so far.

Rob LeVineRob LeVine replied 2 years ago

While this is tough for me to code this solution in absentia, the following seems like it might work, though it's completely untested and not even checked for syntax errors:

// $entry_id needs to be passed in via the frm-condition shortcode
$current_user_id = get_current_user_id();

global $wpdb;
$items_table = $wpdb->prefix . 'frm_items';
$sql = "SELECT user_id FROM $items_table WHERE id = $entry_id";
$userIdFromDynamicField = wpdb->get_col($sql);

return $userIdFromDynamicField == $current_user_id;

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