how to make a custom field readonly or disabled by user role?

First, you can get the current user object like so: $user = wp_get_current_user();.

The $user object would have a roles property containing the user’s roles, so you can do the following to check if the current user has a specific role: in_array( 'role_slug', $user->roles ).

And then in your field HTML, you can use the disabled() and readonly() functions to easily add the disabled and readonly attributes:

<?php
// Get the current user object.
$user = wp_get_current_user();

// Check if the associated user has a specific role.
$has_role = in_array( 'administrator', $user->roles );
?>
<input name="some_name" <?php disabled( true, ! $has_role ); ?> />
<input name="some_name2" <?php readonly( true, ! $has_role ); ?> />

And the above would make the field be disabled/readonly if the current user doesn’t have the role administrator (note the ! $has_role).