1

I am working on a Wordpress website where I am trying to redirect the user after he just registered or logged in but I also want to have access to the error or success messages once on the homepage (I use session variables for that).

For the log, I use the filter authenticate and by applying the filter wp_authenticate_username_password I am able to get the error messages, put them in my session variable and redirect the user.

For the registration, I use the action register_post and by applying the filter registration_errors I am also able to get the error messages and redirect the user.

Although, where I struggle is that I am realizing that the filter authenticate is also called when registering and sending an email when forgotten password. Therefore, when the registration is a success I am getting a login error instead of my success registration message.

I hope my problem is clear to you. How would you do this? Which hook would you use for each?

Thank you very much, regards.

Here is the code (big part of it is where I am customizing the error messages):

add_filter( 'authenticate', 'redirection_login_fail', 99, 3);
add_action('register_post', 'redirect_registration_fail', 29, 3);

function redirect_registration_fail( $sanitized_user_login, $user_email, $errors ){
    // Fetch error messages
    $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    // Create session variable thast will contain eror messages
    $_SESSION['redirect_message'] = array();
    if (is_wp_error($errors)) {
        $new_errors = new WP_Error();
        $codes = $errors->get_error_codes();
        foreach ($codes as $code) {
            switch($code) {
                case 'username_exists':
                    $new_errors->add('username_exists', __('Unfortunately this username is already registered. Please choose another one.'));
                    break;
                case 'email_exists':
                    $new_errors->add('email_exists', __('Unfortunately this email is already registered. Please choose another one.'));
                    break;
                default:
                    $new_errors->add('registration_failed', __('Unfortunately your registration has failed. Please try again.'));
                    break;
            }
        }
        $_SESSION['redirect_message_type'] = 'error';
        $_SESSION['redirect_message'] = $new_errors->get_error_messages();
    }
    else {
        $_SESSION['redirect_message_type'] = 'success';
        $_SESSION['redirect_message'][] = 'Registration complete. Please check your e-mail.';
    }
    // Redirect user
    wp_redirect(home_url());
}

/*
 * Redirection home on fail login
 */
function redirection_login_fail( $user, $username, $password )
{    
    if ( is_a($user, 'WP_User') ) { return $user; }

    $_SESSION['redirect_message'] = array(); // init message
    $_SESSION['redirect_message_type'] = 'error';

    if ( empty($username) || empty($password) )
    {
        $error  = new WP_Error('authentication_failed', __('You must specify a valid username and password.')); 
        $_SESSION['redirect_message'] = $error->get_error_messages();        
    }
    else {
        $errors = apply_filters('wp_authenticate_username_password', $user, $username, $password);
        if (is_wp_error($errors)) {
            $new_errors = new WP_Error();
            $codes = $errors->get_error_codes();
            $messages = $errors->get_error_messages();
            $errs = array();
            for ($i=0;$i<count($codes);++$i) {
                $errs[$codes[$i]] = $messages[$i];
            }
            foreach ($errs as $code => $message) {
                switch($code) {
                    case 'incorrect_password':
                        $matches = array();
                        $pattern = "/<strong ?.*>(.*)<\/strong>/";
                        preg_match($pattern, $message, $matches);
                        $new_errors->add('incorrect_password', __('Unfortunately the password you entered for the username <strong>'.$matches[1].'</strong> is incorrect. <a href="">Forgot your password?</a>'));
                        break;
                    case 'invalid_username':
                        $new_errors->add('invalid_username', __('Unfortunately this is an invalid username. <a href="">Forgot your password?</a>'));
                        break;
                    default:
                        $new_errors->add('authentication_failed', __('Unfortunately these credentials are incorrect. <a href="">Forgot your password?</a>'));
                        break;
                }
            }
            $_SESSION['redirect_message'] = $new_errors->get_error_messages();
        }
    }

    // Redirection
    wp_redirect(home_url());
}
  • I see that you are using $_SESSION and you must now that Wordpres doesn't handle PHP sessions by default. Have you implemented PHP sessions anywhere? – cybmeta Jun 18 '14 at 9:22
  • Well the session variables are not the problem here. This works very fine. – mentinet Jun 18 '14 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.