This is basically the same request as Integrating WordPress to my website, while keeping my own authentication system

I am using the below code as requested.

It seeks authentication from our existing login script, what response is needed that the plugin is looking for from our existing code?

Also Our existing code allows for 2 factor, if the user is not on a particular IP range, a two factor box will popup for an sms code be entered into, then user submits and trys authentication again. how would I hack the plugin to allow for this.

Thanks Larry

   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 = new WP_User($user_id);
        return $user;

    function login_form () {
        echo '' . __('Login with Custom', 'custom_login') . '

    function login_head() {
        // put custom styling here

  • Guys, appreciate the edits, but helping me with an answer would be much more helpful :) – user121816 Jun 15 '17 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.