check for the current screen

i'm working on my core theme plugin, i created a theme options in theme admin page, normally to avoid nonces errors, i must separate the sections with tabbed navigation, so i successfully did it , the idea was to set one section as default when the user enter the theme options page, when i tried to do , the only way to do it is to check on the current screen either with get_current_screen() function or the global variable $current_screen, unfortunately with the classes oop i couldn't achieve it with success . that's my code .

class myClass {

    protected $curScreen;

    public function load_Hooks() {
        if ( is_admin() ) {
            add_action( 'admin_init', array( $this, 'admin_Settins_Pages' ) );
        }

        global $current_screen;
        $this-curScreen = $current_screen;
    }

    public function admin_Settins_Pages() {

        global $current_screen;

        if ( $this-curScreen-parent_base == 'parent-page-slug' || isset( $_GET['tab'] )  $_GET['tab'] == 'head_options' ) {
            add_settings_section( 'handle-id', 'Head Options', array( $this, 'head_section_callback' ), 'parent-page-slug' );
        }
    }

    public function head_section_callback() {
        // Do Something ...
    }
}

if ( class_exists('myClass') ) {
    $instClass = new myClass();
    $instClass-load_Hooks();
}
  • when i use the function get_current_screen() i fall in fatal error ( call to undefined function).
  • when i use the global variable $current_screen i fall in notice error ( trying to call property of non-object).

so please guys, i'm in need for solution, i hope someone can help me work it out. thanks in advance.

Topic screen-options settings-api errors plugins Wordpress

Category Web


Those variables and functions only exist in the admin area, and aren't for frontend use. You will need to use a different mechanism for the frontend.

Additionally:

  • the docs say that you have to call this after the admin_init hook, calling on that hook will always return null
  • the function is not loaded on all admin pages, e.g. the customizer
  • Use it on the current_screen hook or later

When in doubt, read the documentation

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.