Support

Account

Home Forums Front-end Issues Use acf/load_field/ to get user email and populate ACF Field

Helping

Use acf/load_field/ to get user email and populate ACF Field

  • I want acf/load_field/ to prepopulate an email field.

    I have a field called ‘my_user_email’. I reference it here. Easy

    add_filter('acf/load_field/name=my_user_email', 'my_acf_load_field');

    Now I want to prepopulate the field which is blank for some since none of my users – there are 100 of them – have entered their emails. They just have WordPress emails assigned to their user accounts.

    So ideally code would accomplish this: Get current user email (assigned via WP Users) set that email to the ACF ‘my_user_email’ field as a default value. so it’s already filled in.

    something like this – which doesn’t work

    function my_acf_load_field( $field ) {
    	
    	$current_id = get_current_user_id();
    	$current_email = get_the_author_meta('user_email', $current_id);	
    
    	$field['default_value'] = $current_email;
    	return $field;  
    }

    this loads a blank – nothing

    whereas
    $field['default_value'] = $current_email;
    will load something. I don’t want people to have to type their email here. Not good for users – my client will not be happy. Can’t it just prepopulate the field

  • Hi @zc-web

    I am not certain your issue is clear enough. From your description, your code should work just fine.You need to ensure that the method is actually triggered and the $current_mail actually returns the expected value.

    Hope this helps.

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

The topic ‘Use acf/load_field/ to get user email and populate ACF Field’ is closed to new replies.