Using a WordPress login on a different site
This has been updated for WordPress 2.7: Using the WordPress 2.7 login system
I needed a way of letting users in a Wordpress blog be able to login to a different website on the same web host. Googled the idea and found nothing, so worked it out for myself. I’m using 2.6 in this example. It’s all quite straight forward.
First, your site needs to include the wp-config.php file.
include_once($_SERVER['document_root'].”/blog/wp-config.php”);
Then, edit your config file to contain the following extra lines (note, users will have to log in again after doing this):
define(’SITECOOKIEPATH’, ‘/’ );
define(’COOKIEPATH’, ‘/’ );
define(’ADMIN_COOKIE_PATH’, ‘/’);
This makes sure that the login cookies are stored as the root path, so the whole site can access them.
Any page that has included the wp-config.php file can now call functions like:
wp_get_current_user();
is_user_logged_in();
auth_redirect();
wp_get_current_user() returns an object containing current user information.
is_user_logged_in() returns the obvious boolean result.
auth_redirect() is a really handy function that redirects to the login page for the blog, but once logged in will bounce back to the page where the function was called from.
There is also access to the majority of the Wordpress functions (although most are useless here). Most of the useful ones appear in pluggable.php in the wp-includes directory.
Tips
This will go to a logout page and bounce back to the url inside the urlencode function:
wp-login.php?action=logout&redirect_to=”.urlencode($RETURN_URL)
You will have to edit wp-login.php to make a newly registered user bounce back to the external site, but it’s not too complicated:
=======STEP 1=======
BELOW LINE:
if ( !get_option(’users_can_register’) ) {
wp_redirect(’wp-login.php?registration=disabled’);
exit();
}INSERT
if ( isset( $_REQUEST['redirect_to'] ) )
$redirect_to = $_REQUEST['redirect_to'];
else
$redirect_to = “wp-login.php?checkemail=registered”;=======STEP 2=======
REPLACE
wp_redirect(’wp-login.php?checkemail=registered’);
WITH
wp_redirect($redirect_to);
=======STEP 3=======
REPLACE
<form name=”registerform” id=”registerform” action=”<?php echo site_url(’wp-login.php?action=register’, ‘login_post’) ?>” method=”post”>
WITH
<form name=”registerform” id=”registerform” action=”<?php echo site_url(’wp-login.php?action=register&redirect_to=’.$redirect_to, ‘login_post’) ?>” method=”post”>
Now, if you link to the register page with a redirect_to variable it will bounce back to your site afterwards without messing up your blog.
4 Comments »
RSS feed for comments on this post. TrackBack URL
Now this some great info!!! Thanks a lot!
Thank you, I couldn’t find this anywhere online so I worked out a method for myself… this is by no means the most optimized method as you could probably manually include only a few files you need, but I can’t work out which ones and this method lets you use loads of the functions.
Well written article.
Хм, отличная статья