skip to Main Content

note: updated the code with suggested code

I am creating a menu and submenu in the wordpress admin for a plugin.
The menu item of the main menu works fine and goes to the right php file(page).
but the submenu shows the text of the menu but when clicked on it it redirects to the 404 page and in my case it will go to the homepage. I have spend some hours over the code and it looks like I am overseeing something. Here is the code:

class BbtbPlugin{

   public function __construct(){
      add_action( 'init', array( $this,'bbtb_admin_menu' ));
   }
   public function bbtb_admin_menu() {
     add_menu_page(
        'Bricks by the Bay conventions',
        'BBTB conventions',
        'manage_options',
        'bbtb_conventions',
        'all_bbtb_conventions',
        'dashicons-media-spreadsheet',
        11
      );
      add_submenu_page(
        'bbtb_conventions',
        'Settings',
        'Settings',
        'manage_options',
        'bbtb_settings',
         function () { // anonymous callback function
            include "includes/setting_page.php";
        }
       );
       function all_bbtb_conventions() {
        include "includes/add_convention.php";
       }
    }
  }

So I checked if the function of the submenu works by replacing in the add_menu_page the ‘all_bbtb_convention’ with ‘bbtb_settings’. Then the function works.

So there must be something in the add_submenu_page settings I am missing

2

Answers


  1. Chosen as BEST ANSWER

    Figured out I need to change the add_action( 'init', array( $this,'bbtb_admin_menu' )); replace the init with admin_menu. Now it works good


  2. It may be because you are using the same name of the slug in the name of the callback function, try changing it.

    You can also do the following:

    add_submenu_page(
        'bbtb_conventions',
        'Settings',
        'Settings',
        'manage_options',
        'bbtb_settings',
        'bbtb_settings_callback'
    );
    
    function bbtb_settings_callback() {
        include "includes/setting_page.php";
    }
    

    You can even omit the callback function with an anonymous one.

    add_submenu_page(
        'bbtb_conventions',
        'Settings',
        'Settings',
        'manage_options',
        'bbtb_settings',
        function () { // anonymous callback function
            include "includes/setting_page.php";
        }
    );
    

    There is also no need to add a conditional to verify the capacity of manage_options, because for something the capacity is set in parameter 3 in add_menu_page() and in parameter 4 in add_submenu_page()

    I hope I have helped you, greetings!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search