Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I built front-end log in like this.

<?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 
}
?>

here what I want is
if users log in, it will remains on that page and
if admin would log in, it will automatically redirects to /wp-admin/ dashboard.

How can I code like that ?
I'm stuck with if statement. help me .

thanks

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
thanks JMau , it's definitely what I need. –  Ronald Sep 12 '13 at 9:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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