0

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.

2

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

4
  • Thanks Man for your Help , but As you See There is no different Mechanism to use instead of that To Check On The Current Screen , and As you See i Can't use the function add_settings_section in other hook instead of admin_init . do you Have some ways to Leave Default Tab Navigation When The User Enter The Theme options Page ? . Apr 9 '18 at 17:44
  • @SimoPatrek You could initialize your class ON the current_screen hook - you'll lose access to admin_init but I don't see why you need to know the page to register your settings, if you don't want them to display why not just return in the callback if the $_GET value doesn't exist?
    – Howdy_McGee
    Apr 9 '18 at 18:30
  • @SimoPatrek what exactly are you doing? Is this one of those things were you had a problem and your solution didn't work, so instead of asking how to solve the problem, you ask about how to fix your solution instead? You shouldn't need to conditionally add a settings section, just like you don't register post types only when it's being shown, you always register them, and choose what to display as and when you need it. Just like buildings not being demolished when you look away from them, then rebuilt when they come into view
    – Tom J Nowell
    Apr 9 '18 at 19:24
  • @TomJNowell Thanks For your Reply , I Just Want To Display one Section as Default When The User Enter The Theme Options Admin Page . So I Think There No Other Approaches To Do it . But i'll Keep on thinking Of How To Do it . Apr 9 '18 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.