Forked from greathmaster/pmprorh-make-profile-fields-required.php
Last active
February 2, 2020 11:07
-
-
Save ipokkel/f8e90811cefc9c0d7e687b1cdd0e6ac5 to your computer and use it in GitHub Desktop.
Require register helper fields set as required in the edit user form when a user edits or updates their profile. $pmpro #rh #profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This recipe makes required Register Helper fields added to the profile | |
* required in the user edit profile form. | |
* | |
* Requires the Register Helper field atributes profile and required set to true, e.g. | |
* 'profile' => true, | |
* 'required' => true, | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function pmprorh_make_profile_fields_required( $errors, $update, $user ) { | |
global $pagenow; | |
// bail if RH not active | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) || ! is_admin() || 'profile.php' != $pagenow ) { | |
return false; | |
} | |
//bail if administrator | |
if ( current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
$user_id = $user->ID; | |
if ( ! $user_id ) { | |
$user_id = get_current_user_id(); | |
} | |
$profile_fields = pmprorh_getProfileFields( $user_id ); | |
foreach ( $profile_fields as $field ) { | |
$field_name = $field->name; | |
/** | |
* Check that attribute required function_exists. | |
* Check if required attribute is set to true. | |
* Check that field is not empty | |
* If fieldtype is textarea, check that it's not filled with just spaces. | |
* If fieldtype is text, check that it's not filled with just spaces. | |
* If fieldtype is select with multiple attribute, | |
* check that first value in array is not empty. | |
* | |
* Note: splitting "if" conditions across multiple lines for readability | |
*/ | |
if ( isset( $field->required ) && true === $field->required | |
&& ( empty( $_POST[ $field_name ] ) | |
|| ( 'textarea' === $field->type && empty( trim( $_POST[ $field_name ] ) ) ) | |
|| ( 'text' === $field->type && empty( trim( $_POST[ $field_name ] ) ) ) | |
|| ( $field->multiple && empty( trim( $_POST[ $field_name ][0] ) ) ) | |
) | |
) { | |
$error_msg = '<strong>ERROR</strong>: '; | |
// Fall back to field name if no label is set | |
if ( isset( $field->label ) ) { | |
$error_msg .= 'The ' . $field->label . ' field is required. '; | |
} else { | |
$error_msg .= 'The ' . $field->name . ' field is required. '; | |
} | |
$error_msg .= 'Please complete all required fields.'; | |
$errors->add( 'pmprorh_empty_profile_fields', $error_msg ); | |
} | |
} | |
} | |
add_action( 'user_profile_update_errors', 'pmprorh_make_profile_fields_required', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment