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
    – devdarsh
    Commented Jun 13, 2014 at 15:54
  • please check my edited question
    – devdarsh
    Commented Jun 13, 2014 at 16:01
  • The edited code you have above does not produce any error for me, and works as expected.
    – Will
    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.

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