Solved

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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