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?