WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_current_screen() throws 'undefined function' error (7 posts)

  1. Christiaan Conover
    Member
    Posted 9 months ago #

    I'm trying to use the get_current_screen() function in my plugin, but I'm getting an "undefined function" error from PHP when I do. I looked around to see if that function was deprecated but it doesn't appear to be. Any suggestions on how to resolve this?

    You can see the error at https://wp.dev.cconover.com/

  2. bcworkz
    Member
    Posted 9 months ago #

    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.

  3. Christiaan Conover
    Member
    Posted 9 months ago #

    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.

  4. bcworkz
    Member
    Posted 9 months ago #

    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.

  5. Christiaan Conover
    Member
    Posted 9 months ago #

    You can see the script where I'm using it here: https://github.com/cconover/wp-author-customization/blob/dev/author-customization/admin/author-customization-admin.php

    That's the main admin script for my plugin. It's called by the main plugin script only when inside WordPress admin, which you can see here: https://github.com/cconover/wp-author-customization/blob/dev/author-customization/author-customization.php

  6. bcworkz
    Member
    Posted 9 months ago #

    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']?

  7. Christiaan Conover
    Member
    Posted 9 months ago #

    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!

Reply

You must log in to post.

About this Topic