I am trying to use the gform_form_list_forms filter to only display a specific form to a user with a particular role. Unfortunately, I am running into two problems in setting this up.
I am writing the code on my local machine which allows me to trace when a line of code is triggered. The first problem is that add_filter runs, but the break point I set on the referenced function never does. The same goes for the gform_form_list_count filter that I believe also needs to be set. See both below.
add_filter( 'gform_form_list_forms', 'crfcrc_filter_forms', 10, 6 );
function crfcrc_filter_forms( $forms, $search_query, $active, $sort_column, $sort_direction, $trash ){
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
// Get all the user roles as an array.
$user_roles = $user->roles;
$userRole = $user_roles[0];
if ( $userRole == 'memberfield' ) {
$forms = array_slice( $forms, 0, 1 );
}
return $forms;
}
add_filter( 'gform_form_list_count', 'crfcrc_change_list_count', 10, 1 );
function crfcrc_change_list_count( $form_count ){
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
// Get all the user roles as an array.
$user_roles = $user->roles;
$userRole = $user_roles[0];
if ( $userRole == 'memberfield' ) {
$form_count['active'] = 1;
}
return $form_count;
}
Problem #2. The code above was just a test. In order to get the result I want, I assume that I should iterate through the $forms parameter that is passed to the function, find the one form I want, and set $forms to that one element. Unless that is the purpose of the $search_query parameter, but the documentation had no explanation of how to use it. I may be able to sort this out myself once I get the function to fire, but since I am here I figure I would ask.
Thanks for your help.