This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
Use this hook to hide fields depending on certain conditions.
Usage
add_filter('frm_field_type', 'change_my_field_type', 10, 2); function change_my_field_type($type, $field)
Parameters
- $type (string)
- $field (object)
Examples
Hide a field from non-editors
Use this code to hide fields from all user types except editors. The field will still be visible in the admin area.
add_filter('frm_field_type', 'change_my_field_type', 10, 2);
function change_my_field_type($type, $field){
if(in_array($field->id, array(125,126,127))){ //change 125, 126, and 127 to the ids of fields to hide
if(!is_admin() and !current_user_can('editor'))//if current user is not an editor
$type = 'hidden'; //hide the fields
}
return $type;
}
Hide a field when editing
Use this code to hide certain fields when users are editing forms. The fields will still be visible in the admin area when editing entries.
add_filter('frm_field_type', 'change_my_field_type', 10, 2);
function change_my_field_type($type, $field){
if(in_array($field->id, array(125, 126))){ //change 125 and 126 to the ids of the fields to hide
global $frm_vars;
if((!is_admin() || defined('DOING_AJAX')) && isset($frm_vars['editing_entry']) && $frm_vars['editing_entry']) {
//if not in admin area and editing an entry
$type = 'hidden'; //hide this field
}
}
return $type;
}
Hide a field when not editing
Use this code to hide certain fields when users are creating an entry (show field(s) only when editing). The fields will still be visible in the admin area when creating entries.
add_filter('frm_field_type', 'change_my_field_type', 10, 2);
function change_my_field_type($type, $field){
if(in_array($field->id, array(125, 126))){ //change 125 and 126 to the ids of the fields to hide
global $frm_vars;
if((!is_admin() || defined('DOING_AJAX')) && (!isset($frm_vars['editing_entry']) || !$frm_vars['editing_entry'])) {
//if not in admin area and not editing an entry
$type = 'hidden'; //hide this field
}
}
return $type;
}
Hide a field depending on a value in another
add_filter('frm_field_type', 'change_my_field_type', 10, 3);
function change_my_field_type($type, $field, $value){
if($field->id == 25){ //change 25 to the id of the field to conditionally hide
global $my_required_id, $user_ID;
if($my_required_id == $user_ID) //if current user is not an editor
$type = 'hidden'; //hide this field
}else if($field->id == 21){ //change 21 to the id of the field to depend on
global $my_required_id;
$my_required_id = $value; //save the required value to reference when we hit the other field
}
return $type;
}
Hide fields from certain users when editing entries
Use this code to hide fields from specified user when editing entries. The field will still be visible in the admin area.
add_filter('frm_field_type', 'change_my_field_type', 10, 2);
function change_my_field_type($type, $field){
if(in_array($field->id, array(15981, 15980))){ //change 15981 and 15980 to the ids of the fields to hide
$user = wp_get_current_user();
$uid = $user->ID;
$people = array(34, 72, 82); //Change 34,72,82 to the ID of the users you want to hide these fields from
global $frm_vars;
if((!is_admin() || defined('DOING_AJAX')) && isset($frm_vars['editing_entry']) && $frm_vars['editing_entry']) {
//if not in admin area and editing an entry
if(in_array($uid, $people)){
$type = 'hidden'; //hide this field
}
}
}
return $type;
}