WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp signon

Description

Authenticates a user with option to remember credentials. Replaces deprecated function wp_login.

Usage

 <?php wp_signon$credentials$secure_cookie ?> 

Parameters

$credentials
(array) (optional) User info in order to sign on.
Default: None
$secure_cookie
(boolean) (optional) Whether to use secure cookie.
Default: None

NOTE: If you don't provide $credentials, wp_signon uses the $_POST variable (the keys being "log", "pwd" and "rememberme").

Return Value

(object) 
Either WP_Error on failure, or WP_User on success.

Examples

This function and action can be placed in functions.php of the theme. Using the hook init will make it run before the headers and cookies are sent, so it can set the needed cookie for login.

function custom_login() {
	$creds = array();
	$creds['user_login'] = 'example';
	$creds['user_password'] = 'plaintextpw';
	$creds['remember'] = true;
	$user = wp_signon( $creds, false );
	if ( is_wp_error($user) )
		echo $user->get_error_message();
}
// run it before the headers and cookies are sent
add_action( 'init', 'custom_login' );

Notes

  • This function sends headers to the page. It must be run before any content is returned.
  • This function sets an authentication cookie. Users will not be logged in if it is not sent.

Change Log

Since: 2.5.0

Source File

wp_signon() is located in wp-includes/user.php.

Related

See also index of Function Reference and index of Template Tags.