2

I would like to redirect my logged in users automatically to a next page if they press a button on my menu. I would like to make it that if you're not logged in you'll get on a registration page and when you're logged in you'll get on the correct page (which is a gravity form).

How can I do this?

enter image description here

I want them to get redirected when they click on the "plaats een opdracht" page. If not logged in they'll be redirected to a registration page.

0

You can use login redirect filter login_redirect to redirect to the page you want to redirect.

Or you can use if menu plugin to add the conditional menu on login and logout having the same name.

  • I don't know any coding but to clarify myself; when a user is logged in and they click on the "plaats een opdracht" button I want them to automatically get redirected to my form. IF the user is not logged in I would like to redirect them to a page where they can login or register before going to the form. Could you please give me the exact code and where I have to put it? Thanks in advance. – Ries Feb 16 '18 at 17:14
0

The simplest solution (if you are using default WP login/registration) would be adding a link to wp-login.php with the query string parameter "redirect_to" pointing to the URL where logged in users should be redirected.

  • If they're already logged in this will still take them to the login page, not straight through. – Jacob Peattie Feb 16 '18 at 10:45
0

I don't know if your form page is already protected. Just in case it's not, you can use this code to protect your page from 'non-logged in users'. Place both snippets in functions.php.

function sd_protect_pages() {

    if ( is_user_logged_in() ) {
        // do nothing because user is already logged in
    } else {
        if ( is_page( 'page-with-your-form' ) ) {
            wp_redirect( 'url-where-to-direct-to' );
            exit;
        }
    }
add_action( 'init', 'sd_protect_pages' );

If you place this in your functions.php, a user who visits 'page-with-your-form' gets redirected back to this page (after loggin in).

function sd_login_redirect( $redirect_to, $request, $user ) {

    if ( false != $user ) {
        if ( 'page-with-your-form' == $request ) { 
            $redirect_to = 'page-with-your-form';
        }
    }

    return $redirect_to;

}
add_filter( 'login_redirect', 'sd_login_redirect', 10, 3 );

I think this would do the trick.

  • Alright and instead of the 'page-with-your-form' i just put the URL of that certain page in? or what – Ries Feb 17 '18 at 6:45
  • Partially yes. You replace 'page-with-your-form' with the full URL in the $redirect_to line. The line with == $request should only have the slug of the page which contains the form. I see you're Dutch (like me). If you need help with WP in the future, I'm a freelancer, you can always hit me up through my website (see my profile). – Beee Feb 17 '18 at 11:20

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.