Skip to content

Instantly share code, notes, and snippets.

@LMNTL
Last active April 26, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LMNTL/f97d8039ca5da6a700b89438907b076c to your computer and use it in GitHub Desktop.
Save LMNTL/f97d8039ca5da6a700b89438907b076c to your computer and use it in GitHub Desktop.
Add custom validation for Register Helper fields when editing from profile page
/*
Add custom validation for Register Helper fields when editing from profile page.
Requires Paid Memberships Pro and Register Helper Add On.
*/
function my_pmprorh_user_profile_validate_rhfields($errors, $update, $user) {
if( isset( $_POST['my_errors'] ) ){
$my_errors = $_POST['my_errors'];
foreach( $my_errors as $single_error){
$errors->add('my_pmprorh_error',__($single_error));
}
}
}
add_filter( 'user_profile_update_errors', 'my_pmprorh_user_profile_validate_rhfields', 10, 3 );
function my_pmprorh_user_profile_unset_rhfields($user_id) {
$errors = array();
// check to see if form was filled out correctly
if( $_POST['pmprorh_county'] == "Cook" ){
unset($_POST['pmprorh_county']);
// not correct? tell them why
$errors[] = "Not available in Cook County.";
}
if( $_POST['pmprorh_town'] == "Chicago" ){
unset($_POST['pmprorh_town']);
$errors[] = "Not available in Chicago .";
}
if( count($errors) ) {
$_POST['my_errors'] = $errors;
}
}
add_action( 'personal_options_update', 'my_pmprorh_user_profile_unset_rhfields', 5, 1 );
add_action( 'edit_user_profile_update', 'my_pmprorh_user_profile_unset_rhfields', 5, 1 );
function my_pmprorh_init_2()
{
//don't break if Register Helper is not loaded
if(!function_exists("pmprorh_add_registration_field"))
{
return false;
}
//define the fields
$fields = array();
$fields[] = new PMProRH_Field(
'pmprorh_town', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Town', // custom field label
'size' => 30, // input size
'profile' => true, // show in user profile
'required' => true, // make this field required
'addmember' => true
)
);
$fields[] = new PMProRH_Field(
'pmprorh_county', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'County', // custom field label
'size' => 30, // input size
'profile' => true, // show in user profile
'required' => false, // make this field required
'addmember' => true
)
);
//add the fields into a new checkout_boxes are of the checkout page
foreach($fields as $field)
pmprorh_add_registration_field(
"checkout_boxes", // location on checkout page
$field // PMProRH_Field object
);
}
add_action("init", "my_pmprorh_init_2");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment