?
Solved

if ( is_user_logged_in() ) { ... } causes php error in plugin, why, and what can I use?

Posted on 2014-12-19
4
Medium Priority
?
392 Views
Last Modified: 2014-12-20
I have a plugin that I only want logged in users to be able to use.

I opened the plugin's php file and surrounded the script with:

if ( is_user_logged_in() ) { /*script here*/  }

but that causes a php error and gives me a blank page.

I also tried:

get_currentuserinfo() ;

and then did an if based on $user_level >= 1  but that resulted in a blank page as well. In testing, I found that as soon as I put in the get_currentuserinfo() ;  I get the error.

I tested just a general if statement surrounding the script, ie:

$test = 'no';
if ($test == 'test') { /*script here*/ }

and that works, the page loads, the plugin isn't available.

Why would is_user_logged_in()  and  get_currentuserinfo()  cause errors?  And what alternatives are there for me?

Chris
0
Comment
Question by:St_Aug_Beach_Bum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 2000 total points
ID: 40510458
This isn't particularly easy to troubleshoot without the plugin code, but I'll give it a shot.

The reason I think your WordPress-based checks are failing are because they are simple inline checks.  But those functions are part of pluggable.php and that file loads after plugins load.  So essentially you are calling on non-defined functions and you're either generating an error that's suppressed (check your error logs) or you're creating a condition that can never evaluate to true.

The way to avoid this is to wrap your logic inside of a function and then hook that function on init so it fires after the plugin and pluggable are loaded. That should validate the user being logged in and process the rest of your code as intended:

add_action( 'init', 'some_function_that_needs_user_info' );
function some_function_that_needs_user_info() {
    global $current_user;
    get_currentuserinfo();

    // Now do stuff
}

Open in new window


or something like that.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 40510903
Ah - thank you very much - great explanation and plain-spoken just enough that I actually understand exactly what you are saying, while still leaving in all the details I need to know to do it myself :)

I can do this - thank again!

Chris
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 40510944
And in practice, works perfect!
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40511003
Great, glad you got it working.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question