Search code examples
phpwordpresswordpress-plugin-creation

wordpress plugin : call to undefined function add_menu_page() using oop php


hello I'm new to wordpress plugin development I have recently added a menu page to my dashboard using procedural php , and it worked perfectly ,but when I jumped to oop php I have faced this problem "Fatal error: Uncaught Error: Call to undefined function add_menu_page() " I have looked to the same problems but no helped me

`class abc_adding_page{
    public function addingpage(){
        add_menu_page('abcba_products' ,'abcba_products' ,'manage_options' ,'abcba_products', function(){} ,'dashicons-store' ,110);
        return $this ;
}
    public function hooking(){
        add_action('admin_menu' ,  array($this ,'addingpage'));
    }
    }

 $x = new abc_adding_page();

 $x->addingpage()->hooking();

Solution

  • You don't need to run the addingpage method. WordPress works on hooks. Just add a hook and the hook's callback will be run in a needed time when all functions for this functionality loaded.

    class abc_adding_page{
        public function addingpage(){
            add_menu_page('abcba_products' ,'abcba_products' ,'manage_options' ,'abcba_products', function(){} ,'dashicons-store' ,110);
        }
        public function hooking(){
            add_action('admin_menu' ,  array($this ,'addingpage'));
        }
    }
    
    $x = new abc_adding_page();
    
    $x->hooking();