Last active
March 19, 2024 06:10
-
-
Save ahsannayem/199df8c2fe9e963bfeaf35178f71a9eb to your computer and use it in GitHub Desktop.
Get User Role For Fluent Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('fluentform/rendering_field_data_select', 'populateUserRoleAsDropdownValue', 10, 2); | |
add_filter('fluentform/editor_init_element_select', 'populateUserRoleAsDropdownValue', 10, 2); | |
function populateUserRoleAsDropdownValue($data, $form) | |
{ | |
if ($form->id != '678') { | |
return $data; | |
} | |
// Check if the field name is not what you want | |
if (\FluentForm\Framework\Helpers\ArrayHelper::get($data, 'attributes.name') != 'dropdown') { | |
return $data; | |
} | |
// Get current user | |
$current_user = wp_get_current_user(); | |
// Get user role | |
$user_role = ''; | |
if (!empty($current_user->roles)) { | |
$user_role = $current_user->roles[0]; // Get the first role of the user | |
} | |
// Set options with user role as value | |
$options = array( | |
array( | |
"label" => $user_role, // User role | |
"value" => $user_role, // User role | |
"calc_value" => '', // Set accordingly if needed | |
"id" => uniqid(), // Generate a unique ID for each option | |
) | |
); | |
// Merge the options with existing advanced options | |
$data['settings']['advanced_options'] = $options; | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use the code: https://youtu.be/ujB7_ahFrFw