I am using a function that redirects the user to a custom page, rather than wp-login in the event of errors. In my case I am using the Http Referer function to send the user back to the same page because I have the login form in a modal. How can I execute a function on the redirected page, so that the page automatically opens the modal?

This is the code for the redirect in the event of an error:

add_filter('login_redirect', '_catch_login_error', 10, 3);
function _catch_login_error($redir1, $redir2, $wperr_user) {
    $rurl = $_SERVER['HTTP_REFERER'];
    if(!is_wp_error($wperr_user) || !$wperr_user->get_error_code()) return $redir1;
        switch($wperr_user->get_error_code()) {
            case 'incorrect_password': 
            case 'empty_password': 
            case 'invalid_username': 
            default: wp_redirect($rurl); }
        return $redir1;

This code courtesy of http://projectivemotion.com/2011/12/16/redirect-failed-login-attempt-wordpress/#comment-653

The function I want to execute after redirect is:

add_action('genesis_before_post', 'force_open_modal_1');
function force_open_modal_1() { ?>
    <script type="text/javascript">
<?php }

Is this possible?

  • Try add_query_arg( 'key', 'value' ); - then hook into wp_query() with your fn, and switch with get_arg( 'key' ) if you want to trigger or abort.
    – kaiser
    Commented Oct 3, 2012 at 15:56
  • Thanks for the reply, how do I go about hooking the _catch_login_error into a wp_query? I'm a little confused
    – Kyle
    Commented Oct 3, 2012 at 17:18
  • Not "a" wp_query. Into the equally named hook (or maybe into template_redirect or whatever fits).
    – kaiser
    Commented Oct 4, 2012 at 6:33

1 Answer 1


Add a nonce to your redirect url.

See the examples in the codex for wp_nonce_url(). Use wp_verify_nonce() to check for the nonce and run your function to load the modal.

You might also want to check if is_user_logged_in() in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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