WordPress: Why can’t I use is_user_logged_in() after initializing main class on init?

I have a main class for my plugin. It is being instantiated on the init action. I thought this would be sufficient to be able to use WordPress functions like is_user_logged_in(), but I’m getting a fatal error: call to undefined function. Below is my code (simplified). Please see update at the bottom of this question.

fd_main.php

Read More
<?php
Class Find_Do_For_Anspress{

  private function includes(){
    require_once(FIND_DO_FOR_ANSPRESS_DIR.'fd_echo.php');
  }

}//End class

function find_do_for_anspress() {
    $FDClassStart = new Find_Do_For_AnsPress();

}
add_action('init', 'find_do_for_anspress');

?>

fd_echo.php

<?php
if ( is_user_logged_in() ) {
                $current_user = wp_get_current_user();
                echo $current_user;
        } else {
                echo 'Not logged in!';
        }

?>

The error: Fatal error: Call to undefined function is_user_logged_in() in /home/effilxhy/public_html/WP/wp-content/plugins/find-do-for-anspress/fd_echo.php on line 1


Based on answer provided:

//This returns 'exist'. This is outside the main class on the same script.
function test_fn() {
    if ( function_exists('is_user_logged_in') )
        echo 'exists';
    else
        echo 'doesnt exist';
}
add_action('init', 'test_fn');


//This returns 'doesn't exist'. Why doesn't this work?
function test_fn() {
    if ( function_exists('is_user_logged_in') )
        echo 'exists';
    else
        echo 'doesnt exist';
}

test_fn();

So I think I found something conclusive, but I’m not sure what to make of it. I was defining the function inside the class. I was instantiating the class with init, wp_head, and wp_loaded. I was running Find_Do_For_Anspress::test_fn(); right after the class definition. This wasn’t working. However, when I run Find_Do_For_Anspress::test_fn(); inside one of the included files, it returns “exists”, so it works. I tried all of the above action hooks to instantiate my class and they all worked. Why is this? The difference was running the function below my class definition as opposed to running it in an included file.

Related posts

2 comments

  1. Have you tried placing the code directly inside the class without using require_once?

    Also, I think require_once includes the file only the first time the class get created.

    I don’t think the issue is because the function is pluggable. The following code outputs: “exists”

    function test_fn() {
        if ( function_exists('is_user_logged_in') )
            echo 'exists';
        else
            echo 'doesnt exist';
    }
    add_action('init', 'test_fn');
    
  2. Try as follows ..

    add_action( 'wp_head', 'check_user_login' );
    
    function check_user_login() {
    
        if ( is_user_logged_in() ) {
            echo 'You are in login state'; 
        } else {
            echo 'Sorry..!';
        }
    
    }
    

    Please check hook list

Comments are closed.