WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I built front-end log in like this.

<?php if( is_user_logged_in() ) { ?>
Template Name: front page   
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">   
        <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 class="row">
            <label for="password">Password</label><br />
            <input type="password" class="text" name="pwd" id="pwd" required/>
        <span class="submit"><input type="submit" value=" Login " name="submit"></span>
        <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />       

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 .


share|improve this question
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(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

Your Answer


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.