How To: Integrating Pligg beta 8.1.0 with Wordpress 2.0.4 (v1.1)



I’ve managed to get Pliggto use Wordpress authentication and user database. You can see it running on the Game Crafter’s Guild website.

I recently got Pligg using Pubcookie and that formed the basis of this work, however they are very different.

I’ve only tested this with Pligg Beta 8.1.0. I believe future versions may have a better approach for external authentication so I can’t guarantee this will work with future versions.

This approach is very “hacky”. I’ve also been slightly lazy. It assumes that you will use Wordpress’ login, logout and register pages. This will break the existing login, logout and register forms in Pligg. The ultimate solution would allow the Pligg forms to login and logout also on Wordpress and the register form to create new users in Wordpress.

It also does not attempt to merge the user databases from Wordpress and Pligg together. Instead they are left seperate. This means that administrators have two places to manage users. If you want to delete a user, you’ve got to delete them from Wordpress and Pligg seperately.

I’ve done previous external application integrations with Wordpress, most notable with DokuWiki. In these previous case, I was able to allow the use of Wordpress template tags in DokuWiki. However, in the case of Pligg, it is not possible as there are several function name clashes. Pligg and Wordpress are considered CMSs (Content Management Systems) and it is inevitable that function names and variables clash.

Version History

1.1 (19/10/2006)

Stray header redirection in login.php removed.

1.0 (17/09/2006)

First Release

Installation locations

The first thing to do is decide on where you will install Pligg and Wordpress. Pligg must be installed in either a directory on the same level as Wordpress or a subdirectory of Wordpress. In my code examples, I’ve installed Wordpress and Pligg on the same level. I will refer to the Wordpress directory as “wp” and Pligg as “p”. So, for reference, I have Pligg and Wordpress installed like this:

/public_html/wp
/public_html/p

Equally valid would be:

/public_html/wp
/public_html/wp/p

Pligg and Wordpress should be part of the same domain. Otherwise the Wordpress cookies won’t be picked up by Pligg.

Installing the software

Install Wordpress normally and configured it how you will. Make sure it’s all working. Make a not eof your database prefix and site url. You can get your database prefix from ‘wp-config.php’. It is the ‘$table_prefix’ variable. Your site url (or URI if you want to be pedantic), is where you access Wordpress from. You can get the exact value from your “Options”, “General” panel when you log in to Wordpress as admin. Your site url should have no trailing slash. It is important you get this right as you won’t be able to log in to Pligg later otherwise. For example, if you install into a different directory, then your webpage’s url will be different from your WordPress url.

Now install Pligg, but use the same database. Make sure everything is working, in particular that you can log in using the “god” account.

Modifying Pligg

All the work to modify Pligg takes place in ‘libs/login.php’. What we want to do is get Pligg to login using Wordpress cookies/authentication. If the user doesn’t yet exist in Pligg, create it. Also, copy over all the existing user data to the Pligg database. We won’t be using Pligg’s login, authentication or logout any more.

However, we can’t just include wordpress and call wordpress functions. Instead, we’re going to have to do it manually. What I’ve done is added a function called is_user_logged_onto_wp(), which does all the extra work.

Here is the modified ‘libs/login.php’. Make sure to correctly set the $wp_db_prefix to your database prefix (including the underscore) and $wp_siteurl to your Wordpress url.

We want to prevent hackers from using direct URLs to log in. So comment out the normal authentication test, as below.

Pligg also comes preconfigured with one special user account, the “god” account. You can’t delete this account via the admin interface. You can create the “god” account in Wordpress to prevent some user creating it and gaining “god” rights on Pligg. Or if you know sql or have access some sort of web access to your database, you can rename the “god” account to match the username of an existing admin user or delete the user outright.

Required Template Changes

So, now your Wordpress users can log in on Wordpress and then be automatically logged in to Pligg. But you’ve got to point the login, logout and register links to your Wordpress equivelents. If your happy enough modifying the template then your ready to go. However, heres a run down of the minimal changes required to make sure it’s okay.

Lets create a copy of the existing template, mollio-beat. To do this, just make a copy of ‘templates/mollio-beat’ in ‘templates/’ (including all subdirectories). Make sure the permissions are correct, etc. I renamed this copy “gcg1″ (for Game Crafters Guild version1), so I now had a ‘template/gcg1′. I went into the admin interface in Pligg and under the Template configuration, changed ‘mollio-beat’ to ‘gcg1′ and disabled the option that lets users change the template. We don’t want users being able to go back to the old way of authentication.

We can’t use Wordpress template tags. You can always write PHP and SQL code that access the data manually in your database if your that way inclined. This means we have to use direct links.

template/gcg1/header.tpl

Open up ‘template/gcg1/header.tpl’. Look for these lines:

Change them to:

Note: In my installation of Pligg, I’ve updated “my_base_url” (via the admin interface) correctly. I also have Wordpress as a subdirectory of that same base url, so it became a handy shortcut to use ‘{$my_base_url}/wp’ to reference any Wordpress links directly.

header.tpl controls the header of all the Pligg pages so these changes will make sure those login, logout and register links are fixed across the entire Pligg site.

template/gcg1/profile_center.tpl

‘template/gcg1/profile_center.tpl’ controls what users can modify in their profile. We’ll want to disable as much as possible of this as their profiles are overwritten from Wordpress everytime they login.

Some where in the file, add this so users know where to modify their profile:

Delete these lines. Users won’t be able to modify their passwords here, so remove the option:

Now we need to disable the fields that are copied over by wordpress. These are aim, email, yahoo, url and gtalk.

template/gcg1/story_center.tpl

‘template/gcg1/story_center.tpl’ controls the display of individual stories. If a non-logged in user, views a story, they will see a login and logout link!

Replace:

With:

template/gcg1/sidebar/login.tpl

‘template/gcg1/sidebar/login.tpl’ is the sidebar login form you see. Replace it with this:

template/gcg1/sidebar/logged_in.tpl

‘template/gcg1/sidebar/logged_in.tpl’ is the sidebar panel you see when logged in. Replace the logout link:

With something like this:

I also add a link to the Wordpress Admin panel for convience here.

Ready to go!

After all these changes, you should be able to login using Wordpress only and be logged in on your Pligg page too! If you have any comments, feedback or find any bugs, just leave a comment here. Thanks.