Customize the WordPress Login Form with a Required Checkbox Field

We have been working with the American College of Cardiology to develop the online press room website for this year’s annual scientific sessions event, ACC.13. As part of the login process, we wanted to make sure a user has agreed to terms and conditions. We are using WordPress for dynamic content mangement, and I needed to find a way to plug in to the CMS’s existing login architecture.

The solution was elegant and fairly easy to implement. The following code must be added in a plugin file or the theme’s functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// As part of WP authentication process, call our function
add_filter('wp_authenticate_user', 'wp_authenticate_user_acc', 99999, 2);
 
function wp_authenticate_user_acc($user, $password) {
	// See if the checkbox #login_accept was checked
	if ( isset( $_REQUEST['login_accept'] ) && $_REQUEST['login_accept'] == 'on' ) {
		// Checkbox on, allow login
		return $user;
	} else {
		// Did NOT check the box, do not allow login
		$error = new WP_Error();
		$error->add('did_not_accept', 'You must accept the terms and conditions' );
		return $error;
	}
}
 
// As part of WP login form construction, call our function
add_filter ( 'login_form', 'login_form_acc' );
 
function login_form_acc(){
	// Add an element to the login form, which must be checked
	echo '<label><input id="login_accept" type="checkbox" name="login_accept" /> I agree</label>';
}