How To: Wordpress.com and the Simple OpenID PHP Class

[Note: This post contains outdated information. Please visit the project page here for the latest information.]

In my previous post, I told you how to get the Simple OpenID PHP Class working with Yahoo. Now we’re going to focus on Wordpress.com. This turned out to be very easy once I realized the problem.

The Problem:
When you try to authenticate a WordPress OpenID using Simple OpenID you’re redirected to the correct URL, but it just displays your blog page rather than asking you to log in.

The Solution:
After a closer look at the URL we find the problem. The WordPress OpenID server URL takes the form http://username.wordpress.com/?openidserver=1. Fair enough. But the query string that we’ve appended with our request parameters also begins with a ? to set it apart from the rest of the URL. So, all we need to do is check the OpenID server URL we found during discovery for an existing query string. If it already has one, we’ll append our parameters with an &, otherwise we’ll use the ? as usual.

Let’s change the GetRedirectURL method to the following.

function GetRedirectURL(){
    $params = array();
    $params['openid.return_to'] = urlencode($this->URLs['approved']);
    if($this->version == "2.0"){
        $params['openid.ns'] = urlencode($this->openid_ns);
        $params['openid.claimed_id'] = urlencode($this->openid_url_identity);
        $params['openid.realm'] = urlencode($this->URLs['trust_root']);
    }else{
        $params['openid.trust_root'] = urlencode($this->URLs['trust_root']);
    }
    $params['openid.mode'] = 'checkid_setup';
    $params['openid.identity'] = urlencode($this->openid_url_identity);

    if (isset($this->fields['required'])
      && (count($this->fields['required']) > 0)) {
        $params['openid.sreg.required'] = implode(',',$this->fields['required']);
    }
    if (isset($this->fields['optional'])
      && (count($this->fields['optional']) > 0)) {
        $params['openid.sreg.optional'] = implode(',',$this->fields['optional']);
    }
    if(strstr($this->URLs['openid_server'], "?")){
        $urlJoiner = "&";
    }else{
        $urlJoiner = "?";
    }
    return $this->URLs['openid_server'] . $urlJoiner . $this->array2url($params);
}

And that’s it! Our WordPress OpenID should now authenticate properly with Simple OpenID. If it still doesn’t seem to work right, you can download the version that I’m using and see if that helps.

Tags: , , ,

Leave a Reply