Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've developed a php app and I'd like that it's restricted to WP users (use WP like a private area). Is there a way to use WP is_user_logged_in function in a custom file (i.e. www.mysite.com/app.php)? where should I put the folder with the app ? I tried but the page is not recognized.. (.htaccess?)

share|improve this question
3  
"I tried" is a rather vague description. What did you try? Please show us some code. Plus: There're plenty questions around here that show you how to give access to specific pages only when a user is logged in and then there's as well a bunch of plugins that force non logged in users to your registration page with a redirect. –  kaiser Mar 13 '13 at 15:20
    
My code is a simple database management script that I would protect with the WP very safe admin area...so the user must be logged in to access the file. My php files are linked so I can't use plugins to use php codes into WP pages. Also developing a page template don't help me... –  keep dream Mar 14 '13 at 13:02
    
This still is a very vague description. How about answering my questions above? What have you tried? And why can't you use "php codes into WP pages?. So please go and update your question with much, much more information. THanks. –  kaiser Mar 14 '13 at 13:58

2 Answers 2

You'll need to bring your code into the WordPress realm. Easiest way to do it is to create a new template (a standard .php file that will reside inside wp-content/themes/your-theme-name directory) and assign that template to a page (check this page for instructions). Look around this series of guides for more help on templates and how to build one. Your simplest template would look something like this:

<?php
/*
  Template Name: My Custom Template
*/

get_header(); ?>

  <?php if ( is_user_logged_in() ) { ?>

<!-- user logged in, show something to them -->

  <?php } else { ?>

<!-- user not logged in, show error message or forward them to homepage -->

  <?php } // end else ?>

<?php get_footer(); ?>

Once you have a working page with a template, you can incorporate your code into that template and use is_user_logged_in function to do what you need to do.

share|improve this answer

This is impossible to answer definitively without a better description, but I am assuming that app.php is a completely distinct piece of code. In which case, WordPress can't really manage access to it. You will need to incorporate it into WordPress. While this is not the only way to do it I would advise:

  1. Create a custom page template for the app.
  2. In that page template paste your app code or include it.
  3. Create a "Page" from wp_admin->Pages and select your template file as the "Template"
  4. To control access you can include if ( is_user_logged_in()) { logic directly in the template. Do this if you want to provide some kind of "Must be logged message"

    if ( is_user_logged_in()) {
        // your app code
    } else {
       echo "Please don't do that";
    }
    
  5. Or, hook to template_redirect. This will let you redirect to some other page

    function protect_my_app_wpse_90691() {
      if (is_page_template('template-name.php') && !is_user_logged_in()) {
        wp_safe_redirect(get_bloginfo('url').'/wp-login.php'); // or some other page that you prefer
      }
    }
    add_action('template_redirect','protect_my_app_wpse_90691');
    
share|improve this answer

Your Answer

 
discard

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.