Using Login Features in Wordpress

Does anyone have experience writing a custom Wordpress login page using the following features:

wp_signon() and wp_set_auth_cookie() 

found at http://codex.wordpress.org/Function_Reference/

I can not get them to work.

The code looks something like this:

 function login_wordpress($username, $password) { $creds = array(); $creds['user_login'] = $username; $creds['user_password'] = $password; $creds['remember'] = true; $user = wp_signon( $creds, false ); if ( is_wp_error($user) ) { echo $user->get_error_message(); die(); } else { wp_set_auth_cookie( $user, 0, 0); } } 

Did I miss something basic?

+4
source share
4 answers

You need to change this line:

 wp_set_auth_cookie( $user, 0, 0); 

For this:

 wp_set_auth_cookie( $user->ID, 0, 0); 

$user is a WP_User Object, not a user ID.

wp_signon returns WP_Error on failure or WP_User on success.

+5
source

I had a real problem ... finally it worked !! (after several days of experimenting and hitting his head about it)

One thing to make sure that you have not sent any output yet or that the session cookie will not be written, as it should be in the header. Also, if you call wp_signon before the start of the session, there the session information is also lost ... sheesh ... strange, but I both had it. Anyhoo no more ado ...

// Create a new user (for example)

 $userdata = array('user_login'->$username,'user_pass'->$password); $user_id = wp_insert_user($userdata); 

// Make sure the username is updated ... I need this because the hook for user_register is called in wp_insert_user, but this hook is called after creating the user, so he needs the DB cache to work clearly, otherwise the wrong username was set ... Thus , the user was automatically registered for only one page - ridiculous. (This was the Plus Redux register, performing bindings by the way.)

 wp_cache_delete($user_id, 'users'); wp_cache_delete($username, 'userlogins'); $userdata = get_userdata($user_id); $username = $userdata->user_login; 

// Make sure the user session is started.

 $vsessionid = session_id(); if (empty($vsessionid)) {session_name('PHPSESSID'); session_start();} 

// Login Current user

 wp_clear_auth_cookie(); $creds = array(); $creds['user_login'] = $username; $creds['user_password'] = $password; $creds['remember'] = true; $user = wp_signon($creds, false); 

// Check the work

 if (is_wp_error($user)) {$error = $user->get_error_message();} else { wp_set_current_user($user_id); // The next line *really* seemed to help! do_action('set_current_user'); $current_user = wp_get_current_user(); if (is_wp_error($current_user)) {$error = $user->get_error_message();} } if ($error) {echo $error; print_r($userdata); print_r($current_user);} 
+3
source
 current_user(); function current_user() { global $current_user,$user_ID; if(is_user_logged_in()) { echo 'User Logged in '.$user_ID; } else { echo 'No user is logged in< br/>'; custom_login(); } } function custom_login() { $creds = array('user_login' => '<USERNAME>', 'user_password' => '<USERPASSWORD>', 'remember' => true ); $user = wp_signon( $creds, false ); wp_set_current_user($user->ID); wp_set_auth_cookie($user->ID, true, false ); do_action( 'wp_login', '<USERNAME>' ); if ( is_wp_error($user) ) echo $user->get_error_message(); } } 
+1
source

I did this in a project a few years ago, so the Wordpress code was a bit different. But this code worked for me:

 // include the wordpress files necessary to run its functions include('../classpages/wp-config.php'); // this includes wp-settings.php, which includes wp-db.php, which makes the database connection include(ABSPATH . WPINC . '/pluggable-functions.php'); // use wordpress function to create the login cookies // this creates a cookie for the username and another for the hashed password, which wordpress reauthenticates each time we call its wp_get_current_user() function wp_setcookie($user_login, $user_pass, false, '', '', $cookieuser); 

I didn't have to use wp_signon at all, but that could change.

Does an error message appear or what do you see when you run your code?

-1
source

Source: https://habr.com/ru/post/1336118/


All Articles