1

So I am trying to display a working version of the default wordpress login form on one of my pages. There is code in the functions.php file that creates the shortcode and as far as I know it works successfully, however after submitting the form, it simply refreshes the page and doesn't redirect to the url I am specifying...can anyone see why it might not work as it should?

This is the function I put in the functions.php file:

//Login form Shortcode
add_shortcode( 'login-form', 'my_login_form_shortcode' );
/**
 * Displays a login form.
 *
 * @since 0.1.0
 * @uses wp_login_form() Displays the login form.
 */
function my_login_form_shortcode( $atts, $content = null ) {

    $defaults = array(      "redirect"              =>  site_url( $_SERVER['REQUEST_URI'] )
                        );

        extract(shortcode_atts($defaults, $atts));
        if (!is_user_logged_in()) {
        $content = wp_login_form( array( 'echo' => false, 'redirect' => $redirect ) );
        }
    return $content;
}

I am then placing it in the desired page like so:

[login-form redirect="https://myurl.com"]

but instead of redirecting me to the right page, it is instead refreshing the page even though I am now successfully logged in...any ideas?

2 Answers 2

0

The problem you're facing is because of the nature of the WordPress redirect. It uses what is called a safe redirect, namely with the function wp_safe_redirect(). This restricts redirects to local URLs only.

Your work around for this is the following where you jump into the login redirect filter and perform your desired redirection then, before WordPress does it for you.

Keep all your previous code as-is, and add the following:


add_filter( 'login_redirect', 'my_login_form_redirect', 0, 1 );

function my_login_form_redirect( $redirect ) {
    if ( !empty( $_POST[ 'redirect_to' ] ) && filter_var( $_POST[ 'redirect_to' ], FILTER_VALIDATE_URL ) ) {
        wp_redirect( $_POST[ 'redirect_to' ] );
    }
    return $redirect; 
}

0

The wp_login_form is redirect only current site url Redirection otherwise by defult Redirect to current page (if user is admin redirect to admin panel)

function my_login_form_shortcode( $atts, $content = null ) {

      $defaults = array( "redirect" =>  site_url( $_SERVER['REQUEST_URI'] ));

        extract(shortcode_atts($defaults, $atts));
        if (is_user_logged_in()) {               
          echo '<div class="logout"> <p>Hello!<div class="logout_user"> You are logged in and can proceed to the <a href="http://yourcurrenturl.info/">Online Seminar</a>.</div></p><br /><p><a id="wp-submit" class="logout" href="', wp_logout_url(), '" title="Logout">Logout</a></p></div>';
      } else { 
          $args = array(
              'echo'           => true,
              'redirect'       => $redirect,
              'label_log_in'   => __( 'Log in' ),
              'form_id'        => 'demo-login',
              'label_username' => __( 'Username' ),
              'label_password' => __( 'Password' ),
              'label_remember' => __( 'Remember Me' ),
              'id_username'    => 'user_login',
              'id_password'    => 'user_pass',
              'id_submit'      => 'wp-submit',
              'remember'       => true,
              'value_username' => NULL,
              'value_remember' => true
          );
         $content = wp_login_form($args); 
      }
    echo $content;
}

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

[login-form redirect="http://yourcurrenturl.info/any/"]

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.