Here is an example authentication plugin. It will pull information from your php session variables. This is how to use it:
- save it into a file mycustom-auth.php
- modify the constants near the top of the class
- put it into wp-content/plugins/mycustom_auth or wp-content/mu-plugins
- enable it in wp-admin
- log out of WP and log in again. You should see one extra line in the WP login form to take you to your custom authenticator. Click on it, then return to WP to see if you are logged in
/*
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
}
}
?>
$_SESSION
but the tough part is implementing all this into WP. I suppose I would have to go through every file and manually do it, hoping nothing breaks.. – apatik May 4 '16 at 17:09