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

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' ) );
}
  • still not work. what i am doing is – devdarsh Jun 13 '14 at 15:54
  • please check my edited question – devdarsh Jun 13 '14 at 16:01
  • The edited code you have above does not produce any error for me, and works as expected. – Will the Web Mechanic Jun 13 '14 at 16:18

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.