Skip to main content
Improve the question, make it more clear
Source Link
Ronald
  • 1.1k
  • 9
  • 16

Redirect users based on their roles, is_admin() conditional statement

I built front-end log in like thishave a frontend login system.

<?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 whatWhat I want to achieve is -
ifIf users log in at the front page, it will remains on thatat the same page and
if, if admin would log in, it will automatically redirectsredirect to /wp-admin//wp-admin/ dashboard.

How can I code like that ?
I'm stuck with if statement. help me This is my front page login codes.

thanks

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

is_admin() conditional statement

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

Redirect users based on their roles, is_admin()

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 
    }
    ?>
Source Link
Ronald
  • 1.1k
  • 9
  • 16

is_admin() conditional statement

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