Home / Admin / How to Automatically Redirect If User Is Already Logged In
Duplicate Snippet

Embed Snippet on Your Site

How to Automatically Redirect If User Is Already Logged In

10+
Code Preview
php
<?php
/**
 * Automatic redirect from login form if a user is already logged in
 *
 * @link https://wpforms.com/developers/how-to-automatically-redirect-if-user-is-already-logged-in/
 */
 
function redirect_to_specific_page() {
     
    // enter the page ID of the page that contains the login form
    $page_id = 345; // Change page ID
     
    // if the user is on the login form page and the user is logged in then redirect
    if ( is_page($page_id) && is_user_logged_in() && ! current_user_can('edit_post', $post_id) ) {
         
        // enter the URL of the page you would like to redirect them to
        wp_redirect( 'http://www.example-site.com/your-page/', 301 ); 
         
        exit;
    }
}
add_action( 'template_redirect', 'redirect_to_specific_page' );

Comments

Add a Comment