R
R
Run Utochkin2014-02-12 12:58:26
WordPress
Run Utochkin, 2014-02-12 12:58:26

Why is the wp_signon() function on wordpress not authorizing?

Good afternoon!
I use the following code for authorization:

$user['user_login'] = $_POST['log'];
$user['user_password'] = $_POST['pwd'];
$user['remember'] = true;
$signon = wp_signon($user, false);
if (is_wp_error($signon)) echo $signon->get_error_message();
print_r($signon);

If I enter incorrect data - displays an error, as expected. If I enter the correct data, then the function returns the WP_User object, as indicated in the wordpress documentation, but does not authorize.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nowm, 2014-02-12
@Run

This code seems to need to be placed inside a function. And call the function itself before the engine sends the headers to the client. Like this:

function custom_login() {
    $user['user_login'] = $_POST['log'];
    $user['user_password'] = $_POST['pwd'];
    $user['remember'] = true;
    $signon = wp_signon($user, false);
    if (is_wp_error($signon)) echo $signon->get_error_message();
    //print_r($signon);
}

add_action( 'after_setup_theme', 'custom_login' );

Because wp_signon still seems to create some cookies in the process. So it should be called before the moment when all this is sent to the client. And you probably have the code somewhere right in the template when it's too late to call wp_signon.

R
Run Utochkin, 2014-02-12
@Run

Haven't worked with wordpress before. Now understood.
The code:

function custom_login() {
    $user['user_login'] = $_POST['log'];
    $user['user_password'] = $_POST['pwd'];
    $user['remember'] = true;
    $signon = wp_signon($user, false);
    if (is_wp_error($signon)) echo $signon->get_error_message();
    //print_r($signon);
}

add_action( 'after_setup_theme', 'custom_login' );

need to be added to functions.php. All happiness, as it turned out, is in the add_action () function.
Thanks to @Xu4'y for the help!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question