Mailster gives you the option to define custom fields where you can store additional information of your subscribers. Sometimes you need to verify this values with some custom code.

Assuming you have a birthday custom field and like to make sure only people over 18 years subscribe so your newsletter.

1. Create your custom field

Add your custom field on the settings page

2. Assign it to your subscription form

If you like to offer an input field for the birthdate you have to add it on the forms field page. Add it from the available Fields on the right int the left area.

Make sure to check the “required” checkbox.

3. Verify the input with custom code

Now you have to add some custom code to your site to tell Mailster to verify the input. Read more on how to add custom code to your site here.

This snippet hooks into mailster_verify_subscriber filter and compares the given birthdate with the current date. If the user is younger than 18 years old it will return an error and prevents the user to subscribe.

add_filter( 'mailster_verify_subscriber', function( $entry ) {

	if ( isset( $entry['birthday'] ) ) {
		// get timestamp of input
		$timestamp = strtotime( $entry['birthday'] );
		// get timestamp mimimum birthdate
		$minimum_birthdate = strtotime( '- 18 years' );
		// compare timestamps
		if ( $timestamp - $minimum_birthdate > 0 ) {
			return new WP_Error( 'birthday', 'You must be at least 18 years old to get this newsletter!' );
		}
	}

	return $entry;
} );

Once the user tries to submit the form and a birthdate younger than 18 an error message will popup.

Tagged: