You can use the current_user_can()
function to test the current user’s role. I would also recommend making the custom field protected by default and allowing access only to the proper users.
In theory, something like this should work:
function my_exclude_custom_fields( $protected, $meta_key) {
if ( in_array( $meta_key, array( 'email', 'text' ) ) ) {
if ( current_user_can( 'udlejer' ) ) {
// meta key is not protected for user role 'udlejer'
return false;
} else {
// make the meta keys protected by default
return true;
}
}
return $protected;
}
add_filter( 'is_protected_meta', 'my_exclude_custom_fields', 10, 2 );