Php code to show fields in “Edit mode” only to admin
-
I have a request to kindly make. I have in “Default Profile” some fields visible only in “View Mode Only” by the owner and the administrator.
Is there a php snippet that allows to show these fields also in “Edit Mode” but only to the administrator?
Sorry for my English, I hope I explained myself.
Thanks.
-
I tried to write the code below to get what I want, but it doesn’t work. Any suggestions?
add_filter('um_profile_field_filter_hook', 'custom_um_only_view_fields_visibility', 10, 2);
function custom_um_only_view_fields_visibility($output, $args) {
// Controlla se il campo è impostato su "Only View"
if ( isset($args['only_view']) && $args['only_view'] == 1 ) {
// Se siamo in modalità di modifica
if ( is_profile_edit() ) {
// Se l'utente è un amministratore
if ( current_user_can('administrator') ) {
// Mostra il campo in modalità di modifica per l'amministratore
return $output; // Ritorna il campo senza modifiche
} else {
// Nascondi il campo per gli utenti non amministratori in modalità di modifica
return ''; // Nascondi il campo
}
}
// Se siamo in modalità di visualizzazione, mostra il campo a tutti
return $output; // Mostra il campo a tutti in modalità di visualizzazione
}
// Ritorna il campo normalmente per tutti gli altri campi che non sono "Only View"
return $output;
}
// Permetti agli amministratori di modificare i campi "Only View"
add_filter('um_can_edit_field', 'custom_um_allow_admin_edit_only_view_fields', 10, 2);
function custom_um_allow_admin_edit_only_view_fields($can_edit, $meta_key) {
// Se l'utente è un amministratore
if ( current_user_can('administrator') ) {
// Controlla se il campo è impostato su "Only View"
$field = um_get_profile_field($meta_key);
if ( isset($field['only_view']) && $field['only_view'] == 1 ) {
return true; // L'amministratore può modificare il campo
}
}
// Mantieni il comportamento predefinito per tutti gli altri utenti
return $can_edit;
}Thanks
You can try this code snippet, where I have added a custom option
to the Privacy setting in the UM Forms Builder,
which will make the code easier to read and update.add_filter( 'um_field_privacy_options', 'um_field_privacy_options_custom', 10, 1 ); function um_field_privacy_options_custom( $privacy_options ) { $privacy_options['-4'] = 'My custom option'; return $privacy_options; } add_filter( 'um_can_view_field_custom', 'um_can_view_field_custom_4', 10, 2 ); function um_can_view_field_custom_4( $can_view, $data ) { if ( $data['public'] == '-4' ) { $can_view = false; if ( is_user_logged_in() && ( um_is_user_himself() || current_user_can( 'administrator' ) )) { $can_view = true; } } return $can_view; } add_filter( 'um_can_edit_field', 'um_can_edit_field_custom_4', 10, 2 ); function um_can_edit_field_custom_4( $can_edit, $data ) { if ( $data['public'] == '-4' ) { $can_edit = false; if ( is_user_logged_in() && current_user_can( 'administrator' ) ) { $can_edit = true; } } return $can_edit; }
Install by adding the code snippet to your active theme’s
functions.php
file
or use the “Code Snippets” Plugin
https://www.ads-software.com/plugins/code-snippets/@missveronicatv Thanks for the reply. I tried to insert the snippet but it doesn’t work ??.The privacy of the field is set to “my custom option” while the visibility is set to “only view mode”.if I go to the profile I see the fields both as a normal user and as an admin, but if I go to edit profile I don’t see those fields as a normal user (this is fine) and not as an admin as I would like.
Thanks.
Change to “View everywhere” and you will see the fields.
The User profile will see the field in Edit mode but update is not performed.You can add this code snippet also for making the field disabled in Edit mode for the User Profile.
add_filter( 'um_is_field_disabled', 'um_is_field_disabled_custom_4', 10, 2 ); function um_is_field_disabled_custom_4( $disabled, $data ) { if ( $data['public'] == '-4' ) { $disabled = ' disabled="disabled" '; if ( is_user_logged_in() && current_user_can( 'administrator' ) ) { $disabled = ''; } } return $disabled; }
I changed it to “View Everywhere” but it still doesn’t work. Now if I go to edit profile the fields are seen by the user who owns the profile (which shouldn’t be) and by the administrator. I would like them to be seen only by the admin and not also by the owner of the profile.
Thanks.
@missveronicatv I changed line 10 to if ( is_user_logged_in() && current_user_can( ‘administrator’ ) ) and I almost got what I wanted. I’ll give you a summary below the code.
add_filter( 'um_field_privacy_options', 'um_field_privacy_options_custom', 10, 1 );
function um_field_privacy_options_custom( $privacy_options ) {
$privacy_options['-4'] = 'Edit visibile solo da admin';
return $privacy_options;
}
add_filter( 'um_can_view_field_custom', 'um_can_view_field_custom_4', 10, 2 );
function um_can_view_field_custom_4( $can_view, $data ) {
if ( $data['public'] == '-4' ) {
$can_view = false;
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$can_view = true;
}
}
return $can_view;
}
add_filter( 'um_can_edit_field', 'um_can_edit_field_custom_4', 10, 2 );
function um_can_edit_field_custom_4( $can_edit, $data ) {
if ( $data['public'] == '-4' ) {
$can_edit = false;
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$can_edit = true;
}
}
return $can_edit;
}- Now the admin sees these fields both on his profile edit page and on other profiles’ page and also sees them on his profile view page and on other profiles’ page (this is perfect).
The problem is the other users:
- They don’t see the fields in edit of their profile (this is perfect)
- Now they don’t see the fields in view of their profile and in view of other users (this is not good).
I would like these users who are not admins to be able to see these fields in view of their profile, but not in edit of their profile.
I hope I explained myself ??
Thanks.
I got what I wanted by changing the code like this:
// Add custom privacy option
add_filter( 'um_field_privacy_options', 'um_field_privacy_options_custom', 10, 1 );
function um_field_privacy_options_custom( $privacy_options ) {
$privacy_options['-4'] = 'Edit visibile solo da admin';
return $privacy_options;
}
// Control field visibility for non-admins (view mode)
add_filter( 'um_can_view_field_custom', 'um_can_view_field_custom_4', 10, 2 );
function um_can_view_field_custom_4( $can_view, $data ) {
if ( $data['public'] == '-4' ) {
// Allow non-admin users to view their own profile fields unless it's the profile edit page
if ( um_profile_id() == get_current_user_id() && !isset($_GET['um_action']) ) {
$can_view = true;
} elseif ( isset($_GET['um_action']) && $_GET['um_action'] === 'edit' && !current_user_can('administrator') ) {
// Non-admins can't view the field on the profile edit page
$can_view = false;
}
// Always allow admins to view the field
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$can_view = true;
}
}
return $can_view;
}
// Control field editability for non-admins (edit mode)
add_filter( 'um_can_edit_field', 'um_can_edit_field_custom_4', 10, 2 );
function um_can_edit_field_custom_4( $can_edit, $data ) {
if ( $data['public'] == '-4' ) {
// Prevent non-admin users from editing fields in their profile
if ( um_profile_id() == get_current_user_id() && !current_user_can( 'administrator' ) ) {
$can_edit = false;
}
// Allow only admins to edit these fields
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$can_edit = true;
}
}
return $can_edit;
}Thanks.
Very good.
You should also update the lines with
if ( $data['public'] == '-4' ) {
toif ( isset( $data['public'] ) && $data['public'] == '-4' ) {
There are many fields without the Privacy set and PHP dislikes the first code line with error messages to the debug log file.
@missveronicatv You’re right, I hadn’t thought of that.
Thanks.
- You must be logged in to reply to this topic.