• I am working with a plugin and I’m trying to understand why I’m not able to add menu page. Interestingly enough, I acceidentally called the function that’s in charge of adding my pages in my template plugin project and it worked. So does anyone know why the add_menu_page function isn’t working in my plugin but will work in another one.

    (Following function is inside a class called MenuCreation)

    public static function createMenu() {
            $menu_pages = get_option('menu_pages');
            $menu_pages['home'] = add_menu_page(
    			'Welcome to Manager',
    			'Manager',
    			'administrator',
    			'home',
    			'MenuCreation::menuCallback',
    			'dashicons-layout'
    		);
    }

    (Inside a separate Admin class)
    add_action('admin_menu', array('MenuCreation', 'createMenu'));

    • This topic was modified 4 years, 8 months ago by ajidevelop.
Viewing 3 replies - 1 through 3 (of 3 total)
  • I may sound silly but is the plugin active and MenuCreation class is included from where you are calling the hook.

    Thanks.

    • This reply was modified 4 years, 8 months ago by Dnesscarkey.
    Thread Starter ajidevelop

    (@ajidevelop)

    Yes it is. I double checked if it was being loaded with my autoloader and even used a require statement so that’s not the issue. After some more digging I found that the add_action(‘admin_menu’) only works in the main plugin file. When I call it anywhere else it doesn’t load. I can even call the add_action in another main file for another plugin and it will still work but for some odd reason it doesn’t work in other files.

    Moderator bcworkz

    (@bcworkz)

    It wouldn’t be the subordinate page that’s preventing your menu page, there is no problem doing so by itself. It’s likely because your action callback was added after “admin_menu” action had already fired. Perhaps your Admin class was instantiated too late or at least the class method with the add_action() call executed too late.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Having troubles with add_menu_page() function’ is closed to new replies.