I believe this function can only be used for admin screens in the backend, the page it is defined on is not loaded for front end requests. While this appears to be the case, I can’t find definitive evidence at the moment.
I moved the call inside my plugin file that’s only loaded when inside wp-admin, and now the error only loads inside wp-admin.
So much for that idea. Is your script run due to an external request (link clicked), or is it in an action or filter hook? Are you able to call other WordPress functions in this context without error?
It’s sounding like an external request where the WordPress environment is not loaded. There’s a few options to deal with this, one is to make an AJAX request instead of using a simple link.
You’re executing the function when the plugin code is loaded which is way too early. The function needs to be called from some action hook, perhaps ‘admin-init’.
If you must know what page is being loaded that early, maybe you can use $_SERVER['REQUEST_URI']?
Ok, thanks. I figured that since the script it’s in is only called once inside admin it would be sufficient. I’ll try your suggestion, thanks!