检查用户是否已登录,在模板目录中的php文件内

时间:2011-09-06 作者:Richard

我的模板目录中有一个php文件,它从数据库生成一些数据。我只希望我的wordpress用户可以访问此数据。

我尝试使用这行代码,但它给了我一个错误,说“headers ready sent”。

怎么了?或者有没有其他方法来解决这个问题?

<?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!";
 }
?> 

2 个回复
最合适的回答,由SO网友:Tom 整理而成

由于文件位于主题目录中,Wordpress可能不希望用户“直接”访问它。为什么不在wordpress中创建一个页面,为您的PHP页面指定一个模板名称,然后将其指定给在wordpress中创建的新页面。例如,您将有一个名为“仅成员”的新页面,其模板是“位于主题中的php页面”。然后,在代码中,您不必调用博客标题,只需将整个页面包装在“Is user logged in”if else语句中即可。

示例:

<?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(); 
?>

SO网友:Arvind Pal
<?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. 
结束

相关推荐