Skip to content

Instantly share code, notes, and snippets.

@sardbaba
Created May 14, 2013 17:55
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 sardbaba/5578031 to your computer and use it in GitHub Desktop.
Save sardbaba/5578031 to your computer and use it in GitHub Desktop.
Add a checkbox for the terms agreement and hooks into wp_authenticate to allow authentication or to display an error message.
/**
* Add the terms agreement field
*/
function sardbaba_add_login_field() {
?>
<p class="forgetmenot">
<label for="terms-agreement">
<input name="terms-agreement" type="checkbox" id="terms-agreement" value="agree"> <?php _e( 'I Agree to the terms and conditions' ) ?>
</label>
</p>
<br>
<?php
}
add_action( 'login_form', 'sardbaba_add_login_field' );
/**
* This hooks into wp_authenticate (wp-includes/pluggable.php)
*/
function sardbaba_authenticate( $user, $username, $password ) {
// Get the user object to check authentication
$user = get_user_by( 'login', $username );
if ( ! $user ) {
// Return null so wp_authenticate can do standard authentication checks
return null;
}
if ( empty( $_POST['terms-agreement'] ) || $_POST['terms-agreement'] != "agree" ) {
// If user is correct but she has not agreed, replace the user object with an error
$user = new WP_Error( 'denied', __( "<strong>ERROR</strong>: You must agree to the terms and conditions." ) );
// User has not agreed, don't authenticate!
remove_action( 'authenticate', 'wp_authenticate_username_password', 20 );
}
// Returns the user object or the error if not agreed
return $user;
}
add_filter( 'authenticate', 'sardbaba_authenticate', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment