下面是一个验证插件示例。它将从php会话变量中提取信息。使用方法如下:
将其保存到文件mycustom auth中。php修改类顶部附近的常量,将其放入wp-content/plugins/mycustom\\u auth或wp-content/mu-plugins中,在wp-admin中启用它,注销wp,然后再次登录。您应该在WP登录表单中看到一个额外的行,以将您带到自定义验证器。单击它,然后返回WP查看您是否已登录
/*
Plugin Name: mycustom Authenticator
Description: authenticates against custom authenticator which uses php sessions
Version: 1.0
*/
new mycustom_auth();
class mycustom_auth{
const AUTH_URL = \'http://domain.com/mylogin.php\'; // custom auth url
const USERNAME = "username"; // php session variable for username
const EMAIL = "email"; // php session variable for email addreess
const FIRSTNAME = "first"; // php session variable for firstname - optional
const LASTNAME = "last"; // php session variable for lastname - optional
function mycustom_auth() {
add_filter(\'authenticate\', array($this,\'authenticate\'), 20, 3);
add_action(\'login_form\', array($this,\'login_form\'));
add_action(\'login_head\', array($this,\'login_head\'));
//remove_filter(\'authenticate\', \'wp_authenticate_username_password\', 20, 3);
}
function authenticate($user, $username, $password) {
if ( is_a($user, \'WP_User\') ) {
return $user;
}
$uid = $_SESSION[mycustom_auth::USERNAME];
$email = $_SESSION[mycustom_auth::EMAIL];
$firstname = $_SESSION[mycustom_auth::FIRSTNAME];
$lastname = $_SESSION[mycustom_auth::LASTNAME];
if (!isset($_SESSION[mycustom_auth::USERNAME]) || empty($uid)) {
return new WP_Error(\'invalid_username\', __(\'Custom Login Error: php session not set.\'));
}
$user = get_user_by( \'login\', $uid); // could get by email address instead
if (!$user) {
$user = $this->create_user($uid,$email,$firstname,$lastname);
if (!$user) {
return new WP_Error(\'invalid_username\', __(\'Custom Login Error: You are not currently registered user for this site.\'));
}
}
return new WP_User($user->ID);
}
function create_user($username,$email,$firstname,$lastname) {
if ( empty($username) || empty($email)) return null;
$user_id = wp_insert_user(array(\'user_login\'=>$username,
\'user_email\'=>$email,\'first_name\'=>$firstname,\'last_name\'=>$lastname));
$user = new WP_User($user_id);
$user->set_role(mycustom_auth::DEFAULT_ROLE);
return $user;
}
function login_form () {
echo \'\' . __(\'Login with Custom\', \'custom_login\') . \'\';
}
function login_head() {
// put custom styling here
}
}
?>