I'm loading a PHP script in a wordpress page but when the script is run I get this:
Fatal error: Call to undefined function is_user_logged_in()
Code that it tries to run:
<?php
if ( is_user_logged_in() == true ) {
/* Some code */
} else {
/* Some other code */
}
?>
I tried searching for an answer but I couldn't find a working solution.
From what I found on the internet my script is running outside of wordpress and that's why it can't find the function.
Perhaps you are running the code too early as mentioned here: https://wordpress.org/support/topic/fatal-error-call-to-undefined-function-is_user_logged_in
The problem is that is_user_logged_in is a pluggable function, and is therefore loaded after this plugin logic is called. The solution is to make sure that you don't call this too early.
His solution was to wrap the code in another function which is called on init and can be put in your functions.php file:
function your_login_function()
{
if ( is_user_logged_in() == true ) {
/* Some code */
} else {
/* Some other code */
}
}
add_action('init', 'your_login_function');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With