Context
I’m making a big plugin with a complex architecture. I would like in my plugin architecture split pages and menus building on admin side.
So I have these classes : Menu.php
, Submenu.php
and Page.php
, SubPage.php
I would like, when the administrator clik on menu/submenu links, it runs a common function callback inside Menu.php
and Submenu.php
which load only the right page or subpage thanks $_GET['page']
.
Problem
Each submenu link does not bind a subpage anymore.
All href of submenu links are in this format :
https://my-site.org/wp-admin/my-slug-page
instead of :
https://my-site.org/wp-admin/admin.php?page=my-slug-page
Submenu.php
class SubMenu {
public $parent_slug;
public $page_title;
public $menu_title;
public $capability;
public $menu_slug;
public $priority;
public function __construct( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $priority = 10 ) {
$this->parent_slug = $parent_slug;
$this->page_title = $page_title;
$this->menu_title = $menu_title;
$this->capability = $capability;
$this->menu_slug = $menu_slug;
$this->priority = $priority;
// Initialize the component
add_action( 'admin_menu', array( $this, 'add_submenu' ), $this->priority );
}
public function add_submenu() {
$page_hook = add_submenu_page(
$this->parent_slug,
_x( $this->page_title, "page_title", PLUGIN_DOMAIN ),
_x( $this->menu_title, "menu_title", PLUGIN_DOMAIN ),
$this->capability,
$this->menu_slug,
array( $this, "output_rooter" )
);
}
public function output_rooter(){
// check $_GET['page'] value
...
// Load the right SubPage class with the view
$class = strToKamelCase( $_GET['page'] );
new $class(); //<-- extends SubPage.php
...
}
}
The function output_rooter
is never called and I have a 404 error each time I click on a submenu link.
Notice : with menu links it works correctly and all href links are perfect.
Someone has got an idea what it should be ?