Solved

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

Posted on 2014-12-19
4
335 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
  • 2
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
And in practice, works perfect!
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
Great, glad you got it working.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now