0

I'm trying to make a login form. But it doesn't redirect. It shows

"Login success"; but it doesn't redirect. I also tried with:

wp_redirect('myurl');

Which is correct way to redirect in WordPress?

My code is:

global $wpdb;
$username=$_POST['username'];
$password=$_POST['password'];

$query = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$username' AND user_pass='$password'");
        if ( $query != null ) {
            echo "Login success";
header("Location:myurl");

        } else {
            echo "incorrect pass or user";

            return false;
        }


<form action="" method="post"/>
    <input type="text" name="username" placeholder="Username"/> 
    <input type="text" name="password" placeholder="Password"/><br> 
    <input type="submit" name="submit" value="Submit">
</form>

I also tried with this wp_signon:

function custom_login() {
$username=$_POST['username'];
$password=$_POST['password'];
$creds = array();
    $creds['user_login'] =$username ;
    $creds['user_pass'] = $password;

$user = wp_signon( $creds, false );

if ($user)

wp_redirect('myurl');

}
add_action( 'after_setup_theme', 'custom_login' );

  • Where did you get the code from? – Brad Dalton Jun 21 '15 at 12:58
  • I've written it. Is there something that I can use? I also tried with wp_signon but it doesn't work, too. – ci_lover Jun 21 '15 at 12:58
  • Good to see you're having a go. Yes, please see my answer below and mark as accepted and up vote if you like it. – Brad Dalton Jun 21 '15 at 13:01
1

Try this:

 <?php  wp_login_form( array(
        'echo'           => true,
        'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
        'form_id'        => 'loginform',
        'label_username' => __( 'Username' ),
        'label_password' => __( 'Password' ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in'   => __( 'Log In' ),
        'id_username'    => 'user_login',
        'id_password'    => 'user_pass',
        'id_remember'    => 'rememberme',
        'id_submit'      => 'wp-submit',
        'remember'       => true,
        'value_username' => '',
        'value_remember' => false
)); ?> 

2nd Parameter

redirect URL to redirect to. Must be absolute (as in, http://example.com/mypage/).

Recommended: site_url( '/mypage/ ' ).

Default: the current page

Note: You will need to configure the 2nd parameter yourself.

  • Could I use only this function? I also tried with this but it's not working : function custom_login() { $username=$_POST['username']; $password=$_POST['password']; $creds = array(); $creds['user_login'] =$username ; $creds['user_pass'] = $password; $user = wp_signon( $creds, false ); if ($user) wp_redirect('mysiteurl'); } add_action( 'after_setup_theme', 'custom_login' ); – ci_lover Jun 21 '15 at 13:02
  • Did you try my solution? Where did you put the code? – Brad Dalton Jun 21 '15 at 13:04
  • I've download insert php plugin and I write it in my page. I'll try it now. I should write only this code? – ci_lover Jun 21 '15 at 13:05
  • I used your code. I logged in but in admin panel. It didn't redirect me. I want user to be redirected to another page - e.g. yotube. I tried with: 'redirect' => ( 'youtube.com/watch?v=3vB4gpXVtIs' ) , but didn't redirect. – ci_lover Jun 21 '15 at 13:13
  • Yes. Just replace the code in your question with the code in the answer. – James Barrett Jun 21 '15 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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