Skip to content

Instantly share code, notes, and snippets.

@greathmaster
Last active January 30, 2020 09:23
Show Gist options
  • Select an option

  • Save greathmaster/bff496fbbdb8e34e4684b3d988a47699 to your computer and use it in GitHub Desktop.

Select an option

Save greathmaster/bff496fbbdb8e34e4684b3d988a47699 to your computer and use it in GitHub Desktop.
Makes fields added to the profile required if the required => true is set.
/*
Makes fields added to the profile required if the required => true is set.
*/
function pmprorh_make_profile_fields_required(&$errors, $update = null, &$user = null)
{
//admin can leave them blank if he wants
if(current_user_can("manage_options"))
return;
$profile_fields = pmprorh_getProfileFields($user->ID);
foreach($profile_fields as $field)
{
if(isset($field->required) && $field->required == true && empty($_REQUEST[$field->id]) )
{
$error_msg = '<strong>ERROR</strong>: ';
if(isset($field->label))
{
$error_msg .= "The ".$field->label." field is required";
}
else
{
$error_msg .= "Please fill all required fields";
}
$errors->add('pmprorh_empty_profile_fields', $error_msg);
}
}
}
add_action( 'user_profile_update_errors', 'pmprorh_make_profile_fields_required' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment