0

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 places a message, his username is automatically placed in that field. However, that field may not be visible or editable.

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?

EDIT

Ok, I managed to get this far:

function set_acf_field_default_value( $field ) {
    global $current_user;
    get_currentuserinfo();
    switch ( $field['name'] ) {
        case 'name_provivder':
            $field['value'] = '' . $current_user->user_login .'';
            break;
    }

    return $field;
}
add_filter('acf/load_field', 'set_acf_field_default_value' );

This is working great! Now I only have one question.

When the logged in user creating a new post he is able to edit this field. How can I make those fields un-editable? Or can I hide those fields?

EDIT 2

Ok, I managed to get this working:

add_filter('acf/prepare_field/name=name_provivder', '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( 'brons', (array) $user->roles ) ) {
                  $usergroup = 'brons';
               }

    switch($field['_name']) {   
        // Which field are we looking for?  
        case 'name_provivder':
                 if ($usergroup == 'brons') {
                  // Set the CLASS of the field to use css to hide
                  $field['wrapper']['class'] = 'hide';
                   }
            break;
    }
return $field;
}

add_action('admin_head', 'my_custom_fonts');

function my_custom_fonts() {
  echo '<style>
    .hide {
      display:none;
    } 
  </style>';
}

This is hiding this specific field if a user is "brons", is it possible to hide a whole group? Cant fine anything in documentation. Anybody got an idea?

  • Can you try JavaScript – inrsaurabh May 29 '18 at 16:01
0

Yes, it can be done (it's almost always true ;))... The question is how to do this? ;)

And here is the solution... You'll have to use acf/load_field filter:

function set_acf_field_default_value( $field ) {
    switch ( $field['name'] ) {
        case 'name_provider':
            $field['value'] = 'PUT YOUR VALUE HERE';
            break;
    }

    return $field;
}
add_filter('acf/load_field', 'set_acf_field_default_value' );

PS. Of course you can use if statement in there, but you rarely want to prefill only one field, so it's easier to do this with switch.

  • Thanks, I tried this and that worked. So now I only wil have to get the username of the user that is currently logged in and put it there. I will try to figure this out.:) – user2812779 May 30 '18 at 8:18
  • And what do you mean by "username"? Login? Displayname? First name + last name? – Krzysiek Dróżdż May 30 '18 at 8:19
  • Hi, I edited my answer, almost there:) Maybo you know a way to hide those fields? – user2812779 May 30 '18 at 10:04
  • @user2812779 you can hide them using ACF options, I guess. Or just use JS script on the form and make them read-only if the user is logged in (based on body class) – Krzysiek Dróżdż May 30 '18 at 10:06
  • I edited my anser, got it working on user level and it is hiding the field. Do you know how to do this for a whole group? Cant find in anywhere – user2812779 May 30 '18 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.