Menus

The menu SDK is available by calling the menu_page() method. The returned object offers a more readable, logical way of adding items to WordPress menus. Begin by generating the menu_page() object.

$menu_page = $this-menu_page();

You can use the $menu_page object or keep calling menu_page().

From there you can add a base menu. This is taken from the Broadcast WordPress plugin.

$this->menu_page()
 ->callback_this( 'callback_method' )
 ->capability( 'edit_posts' )
 ->menu_slug( 'threewp_broadcast' )
 ->menu_title( __( 'Broadcast', 'threewp_broadcast' ) )
 ->page_title( __( 'Broadcast', 'threewp_broadcast' ) )
 ->icon_url( 'dashicons-rss' );

You can also add a submenu. These method calls are very similar to adding a whole menu. This is taken from the Broadcast Queue add-on.

$this->menu_page()
 ->submenu( 'threewp_broadcast_queue' )
 ->callback_this( 'admin_menu_tabs' )
 ->menu_title( __( 'Queue', 'threewp_broadcast' ) )
 ->page_title( __( 'Broadcast Queue', 'threewp_broadcast' ) );

To commit your changes, you can either add everything to the menu.

$this->menu_page()->add_all();

Or just your submenus.

$this->menu_page()->add_submenus();

Why use only add_submenus? Sometimes you don’t want to create a whole menu item, but just add to an existing.

$this->menu_page()
 ->menu_slug( 'options-general.php' );

$this->menu_page()
 ->submenu( 'plainview_job_importer' )
 ->callback_this( 'admin_menu_tabs' )
 ->capability( 'manage_options' )
 ->menu_title( 'Job Importer' )
 ->page_title( 'Plainview Job Importer' );

$this->menu_page()
 ->add_submenus();

If you want to start over, then treat the menu_page() object like the collection it is!

$this->menu_page()->flush();

Leave a Reply

Your email address will not be published. Required fields are marked *