Support

Account

Home Forums ACF PRO Auto populate custom field with user data

Solved

Auto populate custom field with user data

  • Hi,

    I’m developing a new site where you can log in as a user and post messages yourself (custom post type).

    They can fill in or select the largest part of the data themselves, but a few fields have to be filled automatically and are not visible when placing the message.

    As an example, I have an advanced custom field called “name_provider” with this ID “field_5b0bc22b22606” and I want this field to be filled automatically with the username of the user who is currently logged in.

    When someone submittes a message, his username is automatically placed in that field. However, that field may not be visible or editable on the page were they fill in all the other data / fields.

    Is such a thing possible or is this something that can not be realized?

    I have researched the internet quite a bit but have not come across anything.

    Someone here an idea how I can realize the above?

  • I’m not sure how you’re not showing the field. There are a number of filters that can be used to automatically fill an ACF field, depending on the details

  • Hello John,

    Thanks for the reply.

    My programming skills are not very good so maybe you can help me on my way.

    What I want to realize is that he retrieves users data from the user who is logged in at that moment.

    So for example:

    I have a text field called “name_provider” with id “field_5b0bc22b22606”.

    This field is linked to a Custom Post Type. When a user posts a new message, the username of the logged-in user must automatically be entered in the “name_provider” field. So that when a user posts this new message his username is automaticly in the post.

    Could you help me get started with this?

  • If this is only going to be for logged in users, the I would probably use a user field rather than a text field so that the post can be associated with the user ID. I’d also only show it in the admin, since you’re auto populating it with the current user it should not be something that user could change.

    To not show the field on the front end I would create an acf/prepare_field filter https://www.advancedcustomfields.com/resources/acf-prepare_field/

    
    add_filter('acf/prepare_field/name=name_provider', 'only_show_name_provider_in_admin');
    function only_show_name_provider_in_admin($field) {
      if (is_admin()) {
        return $field;
      }
      return false;
    }
    

    Then I wold use an acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/ to update this field after the post was saved

    
    add_action('acf/save_post', 'update_name_provider_field');
    function update_name_provider_field($post_id) {
      if (is_admin()) {
        // don't run in admin
        return;
      }
      if (get_post_type($post_id) != 'YOUR CUSTOM POST TYPE')) {
        // don't run if not your post type
        return;
      }
      update_field('name_provider', get_current_user_id(), $post_id);
    }
    
  • I actually take that back on the second bit of code. Since the field does not exist yet you must use the field key

    
    add_action('acf/save_post', 'update_name_provider_field');
    function update_name_provider_field($post_id) {
      if (is_admin()) {
        // don't run in admin
        return;
      }
      if (get_post_type($post_id) != 'YOUR CUSTOM POST TYPE')) {
        // don't run if not your post type
        return;
      }
      // use the acf field key here
      update_field('field_01234567', get_current_user_id(), $post_id);
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Auto populate custom field with user data’ is closed to new replies.