Wordpress 5.1,Windows主机上的Ubuntu 18.04 VBox
我创建了一个“示例管理页面”插件
我已经显示了菜单项,但是页面呈现空白,而不是在代码中显示下面建议的html“ Welcome ...”文本。我知道这是一个范围问题,但我不确定如何解决。 是的,我可以将所有内容都放在类和呈现器之外,但是使用类应该是一种好习惯,所以我宁愿这样做。感谢您提供一种正确进行操作的方法。
class EAP_Example_Widget {
function __construct() {
add_action( 'admin_menu', array($this,'my_admin_menu'));
}
function my_admin_menu() {
add_menu_page( 'Example Admin Page', 'EAP Menu', 'manage_options', 'eap-plugin/eap-admin-page.php', 'eap_admin_page', 'dashicons-tickets', '9999' );
add_submenu_page( 'eap-plugin/eap-admin-page.php', 'Example Admin Sub Page', 'EAP Sub Menu', 'manage_options', 'eap-plugin/eap-admin-sub-page.php', 'eap_admin_sub_page' );
}
function eap_admin_page(){
echo '<h2>Welcome To Example Admin Page</h2>';
}
function eap_admin_sub_page(){
echo '<h2>Welcome To Example Admin Sub Page</h2>';
}
}
// Setup and initialize the admin page.
$eap_admin_page_startup = new EAP_Example_Widget();
答案 0 :(得分:0)
It was absolutely a scope issue.
In order to call a function from within another function, I had to self-reference the class EAP_Example_Widget.
For instance, instead of just 'eap_admin_page' for the callable function, it needed to be:
__CLASS__ .'::eap_admin_page'.
See fixed example below. Now it works.
class EAP_Example_Widget {
function __construct() {
add_action( 'admin_menu', array($this,'my_admin_menu'));
}
function my_admin_menu() {
add_menu_page( 'Example Admin Page', 'EAP Menu', 'manage_options', 'eap-plugin/eap-admin-page.php', __CLASS__ .'::eap_admin_page', 'dashicons-tickets', '9999' );
add_submenu_page( 'eap-plugin/eap-admin-page.php', 'Example Admin Sub Page', 'EAP Sub Menu', 'manage_options', 'eap-plugin/eap-admin-sub-page.php', __CLASS__ .'::eap_admin_sub_page' );
}
function eap_admin_page(){
echo '<h2>Welcome To Example Admin Page</h2>';
}
function eap_admin_sub_page(){
echo '<h2>Welcome To Example Admin Sub Page</h2>';
}
}
// Setup and initialize the admin page.
$eap_admin_page_startup = new EAP_Example_Widget();