1

I was using a pretty standard login redirect script in my theme functions, however have learned that it is causing a bug with an integral plugin for my website. In short, within the plugin's front end system user's can register for accounts in a lightbox modal. With my general redirect turned on the modal isn't able to complete registration.

So, what I am trying to do is run my login redirect on all pages EXCEPT one page which I can identify by the slug. This page is auto generated by the plugin so I don't have an ID or anything.

Here is my existing login redirect. It's pretty simple.

add_action('wp_login','auto_redirect_after_login');
function auto_redirect_after_login(){
    wp_redirect(home_url());
    exit();
}

My hurdle is this user registration can occur on an infinite number of pages however they are all "children" of the plugin. Meaning all of them have the same url structure.

All pages that I need to NOT have the redirect work on will be .com/courses/*

Any ideas?

********** UPDATE **********

Here is where I am now, but it doesn't work.

add_action('wp_footer', 'detect_page_for_redirect');
function detect_page_for_redirect(){
    if( !is_singular('course')) {
        add_action('wp_login','auto_redirect_after_login');
    }
}

function auto_redirect_after_login(){
    wp_redirect(home_url());
    exit();
}
  • Thinking through this all the pages I need the redirect to not be active could be found using is_singular('course') Unfortunately I can't seem to get any sort of IF statement to work with this. – C Porter Oct 4 '17 at 11:05
  • what about adding a hidden form input to the login form conditionally and then testing for that to determine whether to apply the redirect. – majick Oct 4 '17 at 11:15
  • I appreciate the idea, but really don't want to modify core plugin files as this is 3rd party. – C Porter Oct 4 '17 at 11:31
  • you wouldn't need to modify plugin files to do this, the problem is how you are doing it now, the is_singular condition will not do anything because it is after the form submission and will no longer be detected as being on a course page. – majick Oct 4 '17 at 12:28
0

You could try something like this to add a custom form field:

add_action('login_form', 'custom_login_field');
function custom_login_field() {
    if (!is_singular('course')) {
        echo "<input type='hidden' name='context' value='notcourse'>";
    }
}

Which is then picked up if it is set and does the redirect on login:

add_action('wp_login', 'auto_redirect_after_login');    
function auto_redirect_after_login() {
    if ( (isset($_POST['context'])) && ($_POST['context'] == 'notcourse') ) {
         wp_redirect(home_url()); exit();
    }
}

So that any login form submitted from a non-course page will redirect to home after login as this seems to be what you are intending.

UPDATE

Switching the logic around might work instead:

add_action('login_form', 'custom_login_field');
function custom_login_field() {
    if (is_singular('course')) {
        echo "<input type='hidden' name='context' value='course'>";
    }
}

add_action('wp_login', 'auto_redirect_after_login');    
function auto_redirect_after_login() {
    if ( (isset($_POST['context'])) && ($_POST['context'] == 'course') ) {
         return;
    }
    wp_redirect(home_url()); exit();
}
  • Ok, so this is probably the correct solution. In testing it, I have learned that my plugin is providing custom login pages which your action obviously isn't able to add fields to. So this doesn't work in my situation, but it is a great answer. – C Porter Oct 4 '17 at 12:55
  • hmmm in that case maybe switching the logic around might work? see updated answer. – majick Oct 4 '17 at 13:25

Your Answer

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

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