Redirect per shortcode if user is logged in

I want to put the wordpress login screen on my homepage. If a user is not logged in I want them to have to enter their username and password into the box (or sign up).. and if they are logged in, I want them to be redirected to the overview page.

Everything works fine if the user is not logged in, but if they are logged in the redirect isn't working..

Can someone please tell me what's wrong with my php code below.

/* add login shortcode */

add_action( 'init', 'my_add_shortcodes' );

function my_add_shortcodes() {

    add_shortcode( 'my-login-form', 'my_login_form_shortcode' );
}

function my_login_form_shortcode( $attr ) {

    if ( is_user_logged_in()) {
    wp_redirect('http://michellemarcus.co.za/thinkingmachines/overview/') ;
}

    if ( ! is_user_logged_in() ) 

    /* Set up some defaults. */
    $defaults = array(
        'label_username' = 'Username',
        'label_password' = 'Password'
    );

    /* Merge the user input arguments with the defaults. */
    $attr = shortcode_atts( $defaults, $attr );

    /* Set 'echo' to 'false' because we want it to always return instead of print for shortcodes. */
    $attr['echo'] = false;

    return wp_login_form( $attr );
}

Topic wp-login-form shortcode login Wordpress

Category Web


I believe you could use template_redirect to handle the redirect step:

function my_page_template_redirect() {
    if ( !is_page( 'overview' ) && is_user_logged_in() ) {
        wp_redirect( home_url( '/thinkingmachines/overview/' ) );
        die;
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.