0

I have a frontend login system.

What I want to achieve is -
If users log in at the front page, it will remains at the same page, if admin log in, it will redirect to /wp-admin/ dashboard.

This is my front page login codes.

<?php if( is_user_logged_in() ) { ?>
    <?php 
    /*  
    Template Name: front page   
    */      
    get_header();
    global $user_identity;
    $redirect = $_SERVER['REQUEST_URI'];    
    ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; // end of the loop. ?>  
    <?php } else { ?>
     <form action="<?php echo get_option('home'); ?>/wp-login.php" method="post"     id="main_comment_form" class="comments-form">   
        <h2>Login</h2>
            <div class="row">
                <label for="username">Username</label><br />
                <input type="text" class="text" name="log" id="log" value="<?php echo     wp_specialchars(stripslashes($user_login), 1) ?>" required/>
            </div>
            <div class="row">
                <label for="password">Password</label><br />
                <input type="password" class="text" name="pwd" id="pwd" required/>
            </div>
            <span class="submit"><input type="submit" value=" Login " name="submit"></span>
            <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />       
     </form>
    <?php 
    }
    ?>

1 Answer 1

0

You could proceed like that :

add_filter("login_redirect", "wpse_113875_login_redirect", 10, 3);
function wpse_113875_login_redirect($redirect_to, $request, $user) {

 if(is_array($user->roles)) 
     if(in_array('administrator', $user->roles)) 
       return site_url('/wp-admin/');

   return home_url();
   }

This will use the WP login system and redirect users according to their role after login. I think this is easier than building your own script in this case. You could easily add some button or link in front-end to the WP login page. Hope this helps.

0

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.