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
His solution was to wrap the code in another function which is called on init and can be put in your functions.php file:
I’ve got my answer.
I needed to add this line of code to my php script:
And that did the trick.