Solved

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

Posted on 2014-12-19
4
347 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

773 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