0

I'm working on my Plugin & i wanted to declare one of the global wordpress variables which is $current_screen To Check whether The user is on the target page or some other page, my Approach was like this , but it didn't work out .

class my_Class {

    public $current_screen;

    public function __construct() {
        global $current_screen;
        add_action( 'current_screen', array( $this, 'Current_Screen') );
    }

    public function Current_Screen() {
         if ( $current_screen->parent_base == 'slug-page' ) {
             // Do Something ...
         }
    }
}

Unfortunately Nothing Happens but Fatal Error . So Please Guys I Wanna Help . Thanks In Advance .

2 Answers 2

6

The $current_screen is passed as a variable to callables hooked from the current_screen hook.

Before this hook, the $current_screen isn't set up, so globalizing the variable won't do anything anyway.

Also, WordPress offers the convenience function get_current_screen() that returns the global $current_screen variable if it exists.

class wpse {
  protected $current_screen;
  public function load() {
    add_action( 'current_screen', [ $this, 'current_screen' ] );
  }
  public function current_screen( $current_screen ) {
    $this->current_screen = $current_screen;
  }
}
add_action( 'plugins_loaded', [ new wpse(), 'load' ] );
7
  • Thanks for Your Help Nathan, But For The function get_current_screen() , i tried to use it directly in the if statement, but i fall in error [ call to undefined function ] . Could you give me a hint how to use it directly inside The Class . Commented Apr 9, 2018 at 15:09
  • 1
    What hook are you trying to use get_current_screen() on? I don't think it's available until admin_init. So if you try to use it in your class constructor, it won't be defined yet. Commented Apr 9, 2018 at 15:14
  • Simply i'm Creating a plugin settings Option , But The options Are created with tabed navigation and i wanted one of them to be the default to be shown one the user enter to the admin options page , so i wanted to check if the user is on the parent_base page then add_settings_section Commented Apr 9, 2018 at 15:27
  • please can you explain to me the thing you explained above about the class loading and the variable , i didn't understand well . thanks in advance . Commented Apr 9, 2018 at 15:28
  • 1
    You should ask a new question with your actual question (your comment starting with "Simply i'm Creating a plugin settings Option". This is an example of the XY problem. Commented Apr 9, 2018 at 16:03
2

It's not enough to call global in __construct(). global only makes the variable available in the current scope, which is just the __construct() method.

To make it available in other methods you need to call it in each method individually:

class MyClass {
    public function __construct() {
        add_action( 'init', array( $this, 'method') );
    }

    public function method() {
        global $global_variable;

         if ( $global_variable ) {
             // Do Something ...
         }
    }
}

The other option is to assign the global variable as a class property, then refer to it in each method with $this->:

class MyClass {
    public $property;

    public function __construct() {
        global $global_variable;

        $this->property = $global_variable;

        add_action( 'init', array( $this, 'method') );
    }

    public function method() {
         if ( $this->property ) {
             // Do Something ...
         }
    }
}

However, as noted by Nathan in his answer, you need to be wary of when your class is instantiated, as it's possible that the global variable will nit be available at that point.

In those cases you would need to use my first example so that the variable is only referenced in a function that is hooked into the lifecycle at a point where the variable has been created.

4
  • The principle here is generally correct for using global variables in classes, but in your specific example with $current_screen, Nathan's approach is better. Commented Apr 9, 2018 at 15:01
  • Thanks Man For Your Help, But When i use it like you show me in the if statement , i face an error tell me that I'm Trying to get property of non-object Commented Apr 9, 2018 at 15:04
  • See Nathan's answer. Your class is probably being loaded before $current_screen is set (and even when it's never set), so it's not available in the construct method. Commented Apr 9, 2018 at 15:10
  • 2
    I'll update my answer to be more a more generic answer for global variables in classes, as the specific current screen example is wrong. Commented Apr 9, 2018 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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