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 );
  • try to add exit(); just after wp_redirect.
    – Kaperto
    Commented Nov 20, 2019 at 13:28
  • 1
    Shortcodes are rendered after the headers are sent. They are just too late for additional headers like you need in a redirect.
    – fuxia
    Commented Nov 20, 2019 at 14:19

1 Answer 1


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/' ) );
add_action( 'template_redirect', 'my_page_template_redirect' );
  • When I add it before the code I already have, I keep getting this error message - Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP. - Do you know why?
    – michelle
    Commented Nov 21, 2019 at 7:30
  • Did you remove the wp_redirect from your shortcode? Commented Nov 22, 2019 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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