Custom field validation in Ultimate Member 2.0

Ultimate Member plugin has some useful field validations for your Register & Profile forms. Since not everything has been included in the list of built-in field validations, Ultimate Member provides a callback function option to extend the functionality for your custom validation.

When you edit a field settings, you will be able to add custom validation using your function. Here’s an example function that validates a PH based mobiler number:

/**
 * Mobile number validation
 */
add_action("um_custom_field_validation_validate_philippines_mobile_number","validate_philippines_mobile_number", 10, 3);
function validate_philippines_mobile_number $key, $array, $args ){

	if( empty( $args[ $key ]  ) ){
		return;
	}
	
	if( ! preg_match('#^(09|639)\d{9}$#', $args[ $key ] ) ){

		UM()->form()->add_error( $key, __('Please enter a valid SMART/Globe/Sun mobile number','ultimate-member') );
	
	}else{ // Valid mobile number

		$has_users = get_users('meta_value='.$args[ $key ].'&meta_key=ph_mobile_number' );
	    foreach ( $has_users as $user) {
	        UM()->form()->add_error( $key, __('This mobile number is already in use and cannot be transferred to another account.','ultimate-member') );

	        return;
	       
	
	    }

	    $has_users = get_users('meta_value='.substr_replace( $args[ $key ], "639", 0, 2 ).'&meta_key=ph_mobile_number' );
	    foreach ( $has_users as $user) {
	        UM()->form()->add_error( $key, __('This mobile number is already in use and cannot be transferred to another account.','ultimate-member') );
	         return;
	         
		}

		$has_users = get_users('meta_value='.substr_replace( $args[ $key ], "09", 0, 3 ).'&meta_key=ph_mobile_number' );
	    foreach ( $has_users as $user) {
	        UM()->form()->add_error( $key, __('This mobile number is already in use and cannot be transferred to another account.','ultimate-member') );
	         return;
	         
		}


	}
							
}

Let’s breakdown the code..

Ultimate Member has an action hook that attaches your custom function to validate strings or integer values: um_custom_field_validation_{your_custom_action_function_name}

The above action hook provides 3 parameters for your reference in validation and code conditions:

$key – returns field name e.g. ph_mobile_number

$array – returns field settings

$args – returns field value e.g. $mobile_number = $args[ $key ];

add_action("um_custom_field_validation_validate_philippines_mobile_number","validate_philippines_mobile_number", 10, 3);
function validate_philippines_mobile_number $key, $array, $args ){
  // do something
}

If the field value is empty, don’t validate.

if( empty( $args[ $key ]  ) ){
		return;
}

All mobile numbers in PH start with 639 or 09 followed by 9-digit number to complete the format of 11 or 12-digit.

So when the user input doesn’t follow the pattern, it will return an invalid error notice:

if( ! preg_match('#^(09|639)\d{9}$#', $args[ $key ] ) ){
    UM()->form()->add_error( $key, __('Please enter a valid SMART/Globe/Sun mobile number','ultimate-member') );
	
}

But if the user input has passed the mobile number validation, the registration or profile update will proceed to the next validation. If you need to check whether the mobile number has been added or being used by other user, you can do that by using the following code:

$has_users = get_users('meta_value='.$args[ $key ].'&meta_key=ph_mobile_number' );

 foreach ( $has_users as $user) {
    UM()->form()->add_error( $key, __('This mobile number is already in use and cannot be transferred to another account.','ultimate-member') );

   return;
	       
  }

 $has_users = get_users('meta_value='.substr_replace( $args[ $key ], "639", 0, 2 ).'&meta_key=ph_mobile_number' );
foreach ( $has_users as $user) {
   UM()->form()->add_error( $key, __('This mobile number is already in use and cannot be transferred to another account.','ultimate-member') );
   return;
	         
}

$has_users = get_users('meta_value='.substr_replace( $args[ $key ], "09", 0, 3 ).'&meta_key=ph_mobile_number' );
foreach ( $has_users as $user) {
  UM()->form()->add_error( $key, __('This mobile number is already in use and cannot be transferred to another account.','ultimate-member') );
return;
}

There are 3 validations for the field key ph_mobile_number. When one of the validations matches the user input in the usermeta table, it will return the error to the form. If you notice the 3 lookups, user may input a mobile number that starts with 09 or 639 – so this condition makes the validation consistent in finding exact mobile numbers.

I hope this can help you with adding a custom the field validation in Ultimate Member forms!

Pixel has turned 1: My first year of fatherhood

A year ago, Kim was at the last week of her pregnancy and was preparing to give birth to our first child. The day before my son was born, I was supporting an environmental event to prepare for “Earth Day” which I planned to attend and give my full support to the movement. My wife didn’t want me to collaborate in any events during that time, especially when we were expecting our baby to arrive. One of my advocacies involves environmental justices and I understand my purpose and capacity in helping a movement which will help the next generation if I keep supporting it in our province. So, I had to attend this event which will benefit my family and the future of our children.

After the successful event, I came home late with my friends; drunk.  She wouldn’t speaking to me when I got upstairs so I decided to sleep downstairs with my two buddies. Around 6am, I was woken by a smack to my face and she told me her water just broke!

Later that day, my son was born. But before I announced in social media that he was delivered, I was still thinking, what name should we call our son? I had 20 combinations of names in my list before our baby had arrived. But the name “Pixel” just appeared in my mind because I wanted his name unique and ensured that every time I work as a programmer, it will always remind me that I work for Pixel!

Here’s a story which fatherhood has taught me so far…

My wife and I have had so many challenges in our marriage, especially during her pregnancy and postpartum period. I have been fighting countless battles which are only visible in my mind. I was diagnosed with severe anxiety and depression thus affecting my work and relationships with my family and people surrounding me. As one of the results, there was a point that I run away for 2 weeks where I was driving without any destination – just waiting for myself to get into an accident. I run away because i didn’t want everyone to feel hurt because i cannot control my mouth or my emotions when i feel overpowered by negativity. I was driving and circling around Lucena to Lopez in 3 straight days where i only slept and ate inside my car at gas stations. I wasn’t able to work or transfer my funds so I came back home on my birthday just to get my laptop and other things i needed.

I knew everyone was upset and mad because of my negligence and lack of communication for days, but i was at the height of my depression where i couldn’t feel anything – it felt so numb. When i arrived home around 6pm, everyone was preparing food to celebrate my birthday, my wife got me a customized birthday cake and everyone in the family pledged their surprises. There was me, I showed them nothing but a tired face.

I was so careless, pathetic, out of my mind. I was numb.

I went upstairs to get my stuff and I saw Kim sleeping, looking very tired in our room. I was in a hurry and the only thing on my mind was to get my laptop and leave the house again as i felt like i couldn’t deal with more drama or confrontations at that time.

I wasn’t able to find my laptop it in our room. I checked my parents’ room and saw Pixel on the bed. He was still awake and smiled at me.

My parents were not home and my sister was taking care of him. I found my laptop and it was hidden in my father’s closet. After i got everything i needed, i decided to leave the house and run away again – still feeling numb and empty inside.

I was driving back to my best friend’s house where i decided to stay. Suddenly, I almost lost control of the car because of my speed at a sharp curve; almost hitting an electric post and concrete barrier.

My tears began to pour when I stopped. I began to scream and punch my face, hurting myself because i suddenly felt that i was losing my mind. I really felt bad for making people surrounding me worried with my decisions that time. I never wanted to leave him or his Mom. I felt that i was the one who should be isolated from them because i didn’t understand what was happening to my mind because i was filled with negative thoughts.

The toughest decision that a father has to make is to sacrifice what matters to himself and to others over the needs of his family.

Therefore, I have cut off everything that will affect my productivity. I no longer associate myself with people who make me feel bad about myself by just reaching out hands, and to stop involving myself in equally divided efforts and just focus on my family especially to my wife and son Pixel.  I should always be there for my son and “Be who you needed when you were younger”.

My son has been a huge role in helping me get  through these difficult times. He gave me a reason to live. I’ve learned that once you are a Father, you need to just keep learning and growing. Just be Dad and everything just falls in place.


Happy Birthday Kim and Pixel!

Finally, a personal website after 10 years in the Web Development Industry!

Welcome to my personal website! Yeah, It’s mine! And here I am working while taking care of my son! I’d like to share some web development tips and code samples here. Please check back soon to see more posts!

Working while taking care of Pixel ( my son ).
Working while taking care of Pixel ( my son ). He likes those rev limiters and Armytrix exhaust sounds coming out of Nissan GTRs.