Analyze the PHP functions in WordPress that control user login and determine user login

WBOY
Release: 2016-07-29 09:00:48
Original
1165 people have browsed it

Login function: wp_signon()

Function introduction:
wp_signon() function is used to authorize users to log in to WordPress and remember the user name. This function replaces wp_login. Enabled from WordPress version 2.5.

Function usage:

<&#63;php wp_signon( $credentials, $secure_cookie ) &#63;> 
Copy after login

Parameter description:

  • $credentials
  • (array) (optional) Login user information.
  • Default: None
  • $secure_cookie
  • (boolean) (optional) Decide whether to use secure cookies.
  • Default: None

Note: If you do not provide $credentials, wp_signon uses the $_POST parameter (key values ​​​​are "log", "pwd" and "rememberme").

Function return value:
(object)
The object WP_Error will be returned if the login fails, and WP_User will be returned if the login is successful. Function instance:


$creds = array();
$creds['user_login'] = 'example'; //wordperss后台用户名称
$creds['user_password'] = '123456'; //wordperss后台用户密码
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
  echo $user->get_error_message();
Copy after login

Source file:

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


Function to determine whether the user is logged in: is_user_logged_in()

Function introduction: The

is_user_logged_in() function determines whether the user is logged in. If the user is logged in, it returns True otherwise it returns False.


Function usage:


<&#63;php if ( is_user_logged_in() ) { ... } &#63;> 
Copy after login

Function parameters:

This function does not accept any parameters.


Return value:

(boolean)

Return True if you have logged in, otherwise return False.

Function examples:

The following examples display the content displayed by logged in users or unlogged users:


<&#63;php
if ( is_user_logged_in() ) {
  echo 'Welcome, registered user!';
} else {
  echo 'Welcome, visitor!';
}
&#63;>

Copy after login

Source file:

is_user_logged_in() is located in the file wp-includes/pluggable.php.


The above has introduced the PHP functions that control user login and determine user login in WordPress, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!