19 November 2009

How to Use WordPress Credentials for a Third Party PHP Application?

This tutorial will just deal with the simple usage of WordPress credentials in a third party app, particularly the login credentials, and not further check with the authority level or role of the user (i.e., subscriber, contributor, author, etc.).

Oftentimes, as we build a community, we often rely on the magnificent WordPress Mu to run that. But, however comprehensive it is, it just lacks something one way or another.

Say, you need an app located at a subdirectory of the main WPMU site, but that you want to limit access to that app/site to "logged in" users only of the WPMU site. There is a great way to do that.

Based on the article by Kenn Wilson, Howto: Display Wordpress content outside of your blog, here's the hack:


Let's install your third-party app at a subdirectory inside WPMU installation. At the top of your third party script (it could be the index.php that calls all the other scripts within the system, better check it out!), put these lines:


<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
if(!is_user_logged_in())
wp_die('<p>You need to <a href="./../wp-login.php">login</a> to view the content.</p><p>You can go back to here after logging in.</p>');
?>


What this code does

The first line of this block is simply a comment describing what we’re doing. The second line tells Wordpress not to display your templates, and the third line is the key to gaining access to the power of Wordpress from an outside page.

Simple as that. Now, go try this in your third party script outside of WordPress and tell us your experience about it.

No comments:

Post a Comment