Moodle and WordPress Single Sign On in 20 minutes – Part 1

It’s time to make WordPress authentication work on Moodle too.

For moodle 3.0, please see the comment from Carlos below, it takes a few changes in order to work on this new version of moodle.

Scenario:

WordPress installation and Moodle installation.
We are going to use the WordPress database in order to authenticate Moodle users. This way, we can sell courses using some wordpress ecommerce plugin and have instant access from customers.

The two sites are on the very same machine but on different domains (actually WordPress is in the “www.domain.tld” and “*.domain.tld” subdomains and Moodle is on another subdomain (courses.domain.tld) so we need to authenticate users in both sites using the very same database table.

The Problem:

The Moodle external autentication plugin does not work with WordPress authentication out of the box. To make it work, it will need a couple of modification very easy to make, don’t worry, even for a non php skilled person.
We will come back on this later on this post.

Let’s start our 20 minutes modification!

Shop list:

  • WordPress Version 3.4.2
  • WordPress database informations
  • Moodle Version 2.0
  • Moodle External Database Authentication Plugin (already in the Moodle default installation)
  • Some coding so, all stuff for ftp things/sftp/ssh things and a decent text editor with some failsafe feature (save a copy before modify etc)

That’s enough to start working on.

Once installed the two systems, you will have to properly configure the external database authentication plugin on Moodle platform. To do this you have to authenticate yourself as site administrator, then on the menubar choose and click on “Site Administration” -> “Plugin” -> “Authentication” -> “Manage Authentication”.

You can use the provided image on the left to quickly find out the links path needed to get there.

 

 

 

Once there you will see a list of all authentication plugin, their status (active or inactive) their priority order and the link to their “settings”. As you can see in the image below, you have to enable and prioritize the plugin in order to make it work. You probably want to disable the self-registration (see picture in bottom part) feature in order to prevent user to signup using moodle platform because, otherwise, all users created within Moodle will not be able to authenticate against WordPress.

This is the list of the moodle authentication plugins. It shows if a plugin is enabled or not, and it exposes a link for each plugin to be configured.

Once you have done, let’s click on the “Setting” link and let’s try to configure this plugin the best we can.

We have to enter all info used by Moodle to read the WordPress database.
Fields are:

  • Host, in my case i will use “localhost” and probably you too unless your db is hosted on a different machine, in that case you have to use the Hostname or ip address of that machine
  • Database, in my case “mysqli” (Please, notice the trailer “i” – mysqli and not mysql).
  • Use sybase quotes, we do not need this to be on so we will leave it on “NO
  • Db name, the name of your wordpress database (get it from wp-config.php)
  • Db User, mysql user that can access WordPress db tables
  • Db Password, mysql password for mysql user
  • Table, the name of the table where username/passwords are stored. The most of the time is “wp_users” unless you changed your table prefix
  • Username Field, the field containing the username, for WordPress: “user_login”
  • Password Field the field containing the password for username for WordPress: “user_pass”.
  • Password Format, we should choose “wordpressformat but there is no such option in this drop down, so what? we will solve this later, don’t worry
  • External db encoding, i use “UTF-8″
  • SQL setup command, you can leave it blank
  • Debug ADOdb, choose “NO
  • Password-change URL, we use the wordpress password recovery page link

These are the needed field to make this thing work, if you want more integration, you can configure the part named “cron syncronization script” and sync information such as first name, surname, user preferences and so.

But now we have to stay focused on the missing dropdown item and find a way to get it out of there.

We need to modify the file in /auth/db/config.html and change this (at line 190)

$passtype = array();
$passtype["plaintext"] = get_string("plaintext", "auth");
$passtype["md5"] = get_string("md5", "auth");
$passtype["sha1"] = get_string("sha1", "auth");
$passtype["internal"] = get_string("internal", "auth");
echo html_writer::select($passtype, "passtype", $config->passtype, false);

to this

$passtype = array();
$passtype["plaintext"] = get_string("plaintext", "auth");
$passtype["md5"] = get_string("md5", "auth");
$passtype["sha1"] = get_string("sha1", "auth");
$passtype["internal"] = get_string("internal", "auth");
$passtype["wordpress"] = "wordpress";
echo html_writer::select($passtype, "passtype", $config->passtype, false);

This modification will make “WordPress” authentication drop down item available, but still not working.
We need to process the password with this class: class-phpass.php [Phpass Website].
This class, can actually process passwords the same way wordpress does, so this is the missing link to make sso work.
Just copy the file class-phpass.php in your /moodle/lib folder
And then add this line right after other requires at the beginning of wp-login.php to moodle/auth/db/auth.php:

require_once($CFG->libdir."/class-phpass.php");

.
To make our modifications work we need to add a few lines of code to the file /auth/db/auth.php, it’s easy. Open the file and go about line 90, you should see something like this:

if ($this->config->;passtype === 'md5') {   // Re-format password accordingly

$extpassword = md5($extpassword);

} else if ($this->config->;passtype === 'sha1') {
$extpassword = sha1($extpassword);
}

Now, let’s do our final modification and right after the lines here, add this code:

else if ($this->config->passtype === 'wordpress') {
$hash =new PasswordHash(8, false);
$rs = $authdb->Execute("SELECT * FROM {$this->config->table}
WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."'");
$check = $hash->CheckPassword( $extpassword, $rs->fields["user_pass"]);
return $check;
}

If you are as lazy, you can download a copy of both file here, but i strongly suggest you to make modification by hand because time goes by and releases change.

Moodle auth with wordpress

Now, finally we can come back to the settings page and add “WordPress” as “Password format“.

All you have to do to try if this works, is to logout from wordpress, signup into wordpress as new user, and then go and authenticate with the same credentials against Moodle. If all work you will be redirected to the user profile page on moodle where you can complete your profile with your informations. 

Incoming search terms:

  • moodle wordpress (30)
  • wordpress moodle integration (26)
  • wordpress moodle (22)
  • moodle single sign on (16)
  • moodle wordpress integration (16)
  • wordpress sso (14)
  • wordpress single sign on (12)
  • wordpress and moodle (11)
  • single sign on moodle (11)
  • wordpress and moodle integration (10)
(Visited 36,396 times, 1 visits today)

Author: Giuseppe Urso

Giuseppe lives in Haarlem now with his shiny dog, Filippa In 1982 received his first home computer, a Commodore 64, followed by Datasette and a 1541 Floppy Disk Drive. In 1999 he installed his first Linux distro (LRH6). In 2006 he switched to Debian as favourite OS. Giuseppe Urso actively sustains the Free Software Fundation and his founder Richard Mattew Stallman, he speaks to people trying to convince them to join the fight now, and about how important is to use Free Software only. He has a job as Infra Specialist at Hippo Enterprise Java Cms an Open Source Enterprise class Content Management System, one of the coolest company ever, in Amsterdam. He's always ready to install Debian on other people computers for free.

104 thoughts on “Moodle and WordPress Single Sign On in 20 minutes – Part 1”

  1. Hi,

    This seems to be great guide and I see that many have succeeded in getting this to work.
    I have followed the steps described and done everything accordingly, edited the auth.php to show wordpress in dropdown, and added the additional code to auth.php and phpass file in lib folder but I cant get this to work.

    I tried making new user in wordpress to see if user amount in moodle authentication plugins page increased, but it didnt.
    Same login and pass result in – invalid login, please try again, in the moodle.

    Im using moodle 3.0.2 and wordpress 4.4.2

      1. I’m using same version as Edgar´s and I’ve made it work correctly just making some changes:

        } else if ($this->config->passtype === ‘wordpress’) {
        $hash =new PasswordHash(8, false);
        $check = $hash->CheckPassword( $extpassword, $fromdb);
        return $check;

        Just look the difference.
        Hope this helps!

  2. Hi,

    Can you please guide me how to pass woo commerce first and last name to moodle…i need user first and last name to show on the auto generated certificates…currently is taking fname as user email id

Leave a Reply

Your email address will not be published. Required fields are marked *