1

am puzzling why can't i access the global variable $menu inside a php class function

class adminMenus {
   public function getWPdefaultmenus(){
      global $menu;
      return $menu;
   }
}

using this code in a plugin file and this function still return NULL.

Edited what i am doing after @Will the Web Mechanic's answer

class adminMenus {
     private $getWPdefaultmenus;
     public function __construct(){
        add_action( 'admin_menu', array( $this, 'getWPdefaultmenus' ) );
      }
      public function getWPdefaultmenus(){
          global $menu;
          $this->getWPdefaultmenus = $menu;
      }
    }

but this returns error:Fatal error: Cannot access empty property

1 Answer 1

1

You need to hook into some action that happens after the global $menu has been set.

as an example:

class adminMenus {
   public function getWPdefaultmenus(){
      global $menu;
      return $menu;
   }
 add_action( 'admin_menu', array( $this, 'getWPdefaultmenus' ) );
}
3
  • still not work. what i am doing is Commented Jun 13, 2014 at 15:54
  • please check my edited question Commented Jun 13, 2014 at 16:01
  • The edited code you have above does not produce any error for me, and works as expected. Commented Jun 13, 2014 at 16:18

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.