Plenty of people seem to have written a lot about how to make WordPress use some other program’s user authentication mechanism, but there seems to be fairly little on how to get at WordPress’s user authentication from some other program. Fortunately, I found this article, and got what I wanted.
It’s a fairly straight-forward process. At its simplest:
-
auth_redirect();
Including wp-config.php (you may have to watch the path) gets you just about all of WordPress and auth_redirect() will check if the user is logged in to WordPress and if not, they get bounced to a login form.
Where things get trickier is if you want to use the authentication on a subdomain (you have to tweak COOKIE_DOMAIN in wp-config.php [to override what’s already in wp-settings.php) or if your blog is in a subdirectory and you want the authentication outside that subdirectory (try tweaking COOKIEPATH).
Oh, and if you try to put the require_once() statement inside a function, you will also need
global $wpdb;
or nothing will work.
The issue of how much memory it consumes to load all of WordPress just to authenticate users is a whole separate issue.
Comments 1
Please note that this doesn’t quite work with WP2.6, due to changes in the login cookies. See my post on authenticating with WP2.6.
Trackbacks & Pingbacks 3
[...] implementing other pages that used WordPress to authenticate users and deal with access control, I went to move these pages off to a subdomain, and suddenly found that auth_redirect wasn’t [...]
[...] use WordPress as the backbone of a site I run, including using it for user authentication. This means a lot of people who aren’t invovled in running the site are logging in and [...]
[...] as I can tell, this immediately breaks any attempt to use auth_redirect() for authentication (e.g. this and this) outside the /wp-admin and /wp-content/plugins directories. It is also not immediately [...]