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 have a php file in my template directory which generates some data from a database. I only want this data to be accessable for my wordpress users.

I tried by using this line of code but it gives me an error saying "headers already sent".

What is wrong? Or is there any other way to solve this?

<?php

 require_once('../../../wp-blog-header.php');

 if (is_user_logged_in())
 {

    echo "hello world";

 }
 else
 {
    echo "You do not have access to this resource!";
 }
?> 
share|improve this question
    
why not use TEMPLATEPATH constant? –  Sisir Sep 6 '11 at 12:20
    
The code is working for me with no error. I suppose a problem is within other parts of your code. Try to "google" for how to solve a "headers already sent" problem. –  JohnnyPea Sep 6 '11 at 12:27
    
Got it sorted now! Thanks! –  Richard Sep 7 '11 at 7:22

2 Answers 2

Since the file is in your theme directory, Wordpress may not want the user to "directly" access it. Why not create a Page in wordpress, assign a Template Name to your PHP page and then assign that to the new page created in wordpress. For example, you would have a new page called "Members Only" and its template is "php-page-located-in-theme". Then, in the code, you don't have to call the blog header, just simply wrap the entire page in your "Is user logged in" if else statement.

Example:

<?php
Template Name: php-page-located-in-theme

get_header();

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
    /*PHP Generated Content Goes Here*/
} else {
    echo 'You cannot access this page.';
}
get_sidebar();
get_footer(); 
?>
share|improve this answer
    
You are wrong, the path is correct. He said the file is located in his theme directory root/wp-content/themes/theme/file.php –  JohnnyPea Sep 6 '11 at 12:35
    
Ah! Stupid me, will amend my answer then. –  Tom Sep 6 '11 at 12:39
<?php
require_once('../../../wp-blog-header.php');
global $wpdb;
global $current_user,$user_ID;
get_currentuserinfo();
if($user_ID=="")
{
echo "You do not have access to this resource!";
}
else
{
    echo "hello world";
}
?>
You can use global variable global $current_user,$user_ID for checking user is login or not. 
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.