Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

726 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