Prevent User From Editing First and Last Name
If you are using Learndash, TutorLMS, LearnPress, or any other WordPress LMS plugin and provide Certificates to your student, you probably need to prevent students from editing their name after the first time inputting to make sure no one can forgery the certificate.
You can do that by using the following snippet which checks if the first name or last name has a previous value then it will prevent edition that value for non-administrator user
// prevent user from editing first name if it has previous value
add_filter( 'update_user_metadata', function( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
if ('first_name' == $meta_key){
$current_first_name = get_user_meta(get_current_user_id(), 'first_name', true);
if(!current_user_can('administrator')){
if($current_first_name){
return false;
}
}
}
return $check;
}, 10, 5 );
// prevent user from editing last name if it has previous value
add_filter( 'update_user_metadata', function( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
if ('last_name' == $meta_key){
$current_last_name = get_user_meta(get_current_user_id(), 'last_name', true);
if(!current_user_can('administrator')){
if($current_last_name){
return false;
}
}
}
return $check;
}, 11, 5 );