Support

Account

Home Forums Front-end Issues Hide a specific field frontend

Solved

Hide a specific field frontend

  • Hello there,
    I am ACF PRO latest version to build a frontend form. Everything seems working fine, but the forms not getting proper styles in the front end.

    Also, I need to hide a specific field ( required in the backend ) that is available for admin only.

    So I need help with 2 things:

    1. Frontend form CSS style not working properly. ( https://drive.google.com/open?id=0B_X6E_wvTyCRQy0wY1FhdlE3cUE )
    2. Hide a specific field frontend that is required in the backend. ( https://drive.google.com/open?id=0B_X6E_wvTyCRR3dVcXhWMmlWcE0 )

    Thanks in advance

  • 1. Have you added acf_form_head() and does your theme contain the required wp_footer() call?

    2. There are several methods to do this. The first is that acf_form() lets you specify a list of fields that should be shown instead of showing all of them. The second is that you can use an acf/prepare_field filter https://www.advancedcustomfields.com/resources/acf-prepare_field/. You can check to see if the form is shown on the front end and if it is return false from your filter and the field will not be shown.

  • Aziz,

    Here is an option for your second question.

    I use the following code to disable a field if a user doesn’t need to see it. You make your logic use any of the WordPress functions to determine when are where to see it.

    
    add_filter('acf/prepare_field/name=record_number', 'disable_acf_prepare_field');
    
    function disable_acf_prepare_field( $field ) {
      		
            // Does the user have an Office role?
            $user = wp_get_current_user();
                   if ( in_array( 'office', (array) $user->roles ) ) {
    	              $usergroup = 'office';
                   }
    
    	switch($field['_name']) {	
    		// Which field are we looking for?	
    		case 'record_number':
                       if ($usergroup == 'office' {
    			      // Set the CLASS of the field to use css to hide
    			      $field['wrapper']['class'] = 'hide';
                       }
    		    break;
    		default:
                        break;
              	
    	}
    }
    
    return $field;
    
    }
    
    

    Then at the top of my php I include the following css style. Can be either added to the page template or some other theme css that is used for the page/post/post-type.

    <style>
    
    	.hideme {
    		
    		display: none;
    	}
    
    </style>

    Hope this helps.

    Jeff

  • Thanks John and Jeff,

    So the $field parameter is actually an array like below:

    array(23) {
      ["ID"]=>
      int(0)
      ["key"]=>
      string(15) "_validate_email"
      ["label"]=>
      string(14) "Validate Email"
      ["name"]=>
      string(20) "acf[_validate_email]"
      ["prefix"]=>
      string(3) "acf"
      ["type"]=>
      string(4) "text"
      ["value"]=>
      string(0) ""
      ["menu_order"]=>
      int(2)
      ["instructions"]=>
      string(0) ""
      ["required"]=>
      int(0)
      ["id"]=>
      string(0) ""
      ["class"]=>
      string(0) ""
      ["conditional_logic"]=>
      int(0)
      ["parent"]=>
      int(0)
      ["wrapper"]=>
      array(4) {
        ["width"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["id"]=>
        string(0) ""
        ["style"]=>
        string(24) "display:none !important;"
      }
      ["_name"]=>
      string(15) "_validate_email"
      ["_prepare"]=>
      int(1)
      ["_valid"]=>
      int(1)
      ["default_value"]=>
      string(0) ""
      ["maxlength"]=>
      string(0) ""
      ["placeholder"]=>
      string(0) ""
      ["prepend"]=>
      string(0) ""
      ["append"]=>
      string(0) ""
    }

    I have been able to do some awesome thing :

    function viz_acf_prepare_field($field){
    
    	$userid = get_current_user_id();
    	$user = new Viz_Author($userid);
    
    	if ( ! is_admin() ) : 
    		if ( $field['_name'] == 'client' || $field['_name'] == 'template'  ) {
    			return false;
    		}	 
    		if ( $field['_name'] == 'project_template' ) {
    			$field['disabled'] = 1;
    			$field['wrapper']['class'] = 'viz_hidden';
    			$field['value'] = $user->template;
    		}
    	endif; 
    	return $field;
    }
    
    add_filter('acf/prepare_field', 'viz_acf_prepare_field');

    In the above code, client and template don’t display at the front-end form, and project_template is disabled with a selected value and custom class.

    Thank you all

  • Glad to help!

    Make sure you mark this as solved so others can benefit from what you came up with.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Hide a specific field frontend’ is closed to new replies.