WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a site based on WordPress. This site has some functions for member only. One of the functions is a external script and I believe anyone can access this without authentication.

I can add .htaccess to this script to prevent unauthorized access. But then my users have to login again and it's kind of inconvenient.

Is there any solution to add some check to my script that use the same WordPress authentication mechanism, that is, if user already login, he have full access to the script. If not, he will be redirected to WordPress login page.

EDIT: My script is written in PHP

share|improve this question
When you say script, do you mean PHP code or javascript? – Steven Aug 21 '11 at 10:32
Yes, it's PHP code – tuananh Aug 21 '11 at 10:46
up vote 7 down vote accepted

Just add this at the top of your file:


// If you run multisite, you might nees this to prevent 404 error
header('HTTP/1.1 200 OK');

Note that the file must be in the same folder as your theme.

Then you can use is_user_logged_in before executing the rest of the script. If use is not logged in, then just trigger the login form or a login plugin.

share|improve this answer
Thank you :) It works! – tuananh Sep 3 '11 at 11:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.