Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have added an extra login field to the main wordpress login page. It is a checkbox which must be checked (to accept terms & conditions), otherwise the user cannot log in.

I have taken code from this question which has allowed me to add a custom checkbox to the login. However, trying to modify the code given to validate whether the checkbox has been checked, I cannot get to work.

function check_checkbox($user) {
    $acceptance = $_POST['terms_acceptance'];

    $user = get_user_by('login', $username );

    if( !$user || !isset($acceptance) ){
        remove_action('authenticate', 'wp_authenticate_username_password', 20);

        $user = new WP_Error( 'denied', __("<strong>ERROR</strong>: You're unique identifier was invalid.") );
    }

    return null;
}
add_filter( 'wp_authenticate_user', 'check_checkbox', 10, 3 );

I have tried the 'authenticate' & 'wp_authenticate_user' filters as shown, but neither have worked for me.

Can anyone tell me what I'm doing wrong?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Here's the full working code, put it in functions.php:

add_action('login_form','my_added_login_field');
function my_added_login_field(){
?>
    <p>
        <label for="my_extra_field">I agree to the terms<br />
        <input type="checkbox" value="1" class="input" id="my_extra_field" name="my_extra_field_name"/></label>
    </p>
<?php
}

function check_checkbox($user, $password)
{
    if( !isset($_POST['my_extra_field_name']) )
     {
        remove_action('authenticate', 'wp_authenticate_username_password', 20);
        $user = new WP_Error( 'denied', __("<strong>ERROR</strong>: Please agree to our terms.") );
    }

    return $user;
}
add_filter( 'wp_authenticate_user', 'check_checkbox', 10, 3 );
share|improve this answer
    
Thank you. This has worked. It seems I needed to simplify my code. –  Eoghan OLoughlin Oct 7 '13 at 16:51
    
If it worked, please consider upvoting the comment for future users to see. –  P-S Oct 7 '13 at 17:16
    
I will when I get a better reputation, but it's early days for me here and unfortunately I do not have the required reputation to up vote. –  Eoghan OLoughlin Oct 7 '13 at 21:19

Per the Codex...

add_filter('wp_authenticate_user', 'myplugin_auth_login',10,2);
function myplugin_auth_login ($user, $password) {
     //do any extra validation stuff here
     return $user;
}

You need to return the $user object. Your function always returns null. This is completely untested but it looks like you just need to use return $user; on the last line instead of return null;

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.