Context
我正在制作一个具有复杂架构的大插件。我想在我的插件架构中,在管理端拆分页面和菜单。
所以我有这些课程:Menu.php
, Submenu.php
和Page.php
, SubPage.php
我想,当管理员点击菜单/子菜单链接时,它会在其中运行一个公共函数回调Menu.php
和Submenu.php
只加载正确的页面或子页面谢谢$_GET[\'page\']
.
Problem
每个子菜单链接不再绑定子页面。
所有子菜单链接的href均采用以下格式:
https://my-site.org/wp-admin/my-slug-page
而不是:
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
...
}
}
功能
output_rooter
从未调用,每次单击子菜单链接时都会出现404错误。
注意:使用菜单链接,它可以正常工作,所有href链接都是完美的。
有人知道应该是什么?
SO网友:J.BizMai
我找到了解决办法。
Problem of hrefs
当wordpress内核找不到函数回调或函数为空时
add_menu_page()
和
add_submenu_page()
href将如下生成:
http(s)://my-site.org/wp-admin/my-slug-page
, 否则,href的格式为:
http(s)://my-site.org/wp-admin/admin.php?page=my-slug-page
所以在我的例子中,我必须确保回调函数被调用。
Problem to bind the menus with page and load only the current page
我必须在中调用静态公共函数
add_submenu_page()
它位于我的页面类中。
任何页面类都可以扩展Page.php
.
任何子页类都会扩展SubPage.php
.
初始化任何页面/子页面的静态函数对所有页面都是相同的。
所以我PageTrait.php
用于Page.php
和SubPage.php
.
代码如下:
SubMenu.php
class SubMenu {
public $parent_slug;
public $page_title;
public $menu_title;
public $capability;
public $menu_slug;
protected $subpage_class; //<-- new argument used for the submenu function callbak
public $priority;
public function __construct( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $subpage_class, $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->subpage_class = $subpage_class;
$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->subpage_class, "init_page" )
);
}
}
PageTrait.php
trait PageTrait {
public static function init_page(){
//Code to init the right page
//1.Get data for the constructor
...
//2.Get the class path
...
new $classPath( $data );
}
}
Page.php
class Page {
use PageTrait;
....
}
SubPage.php
class SubPage{
use PageTrait;
....
}