(更新答案)
查看simplified plugin source, 以下工作:
// In class-init-test.php
public function __construct(){
add_action( \'admin_init\', array( $this, \'page_init\' ) );
...
}
因为当
add_action()
被称为
admin_init
尚未触发操作。因此
Init_test::page_init()
被调用。
但是,以下操作不起作用,因为admin_init
在你打电话的时候已经被解雇了add_action( \'admin_init\', ... )
:
输入class-init-test-home.php
:
public function __construct() {
add_action( \'admin_init\', array( $this, \'save\' ) );
...
}
英寸class-init-test-submenu.php
:function save(){
...
}
add_action( \'admin_init\', \'save\' );
如果要确认admin_init
是否已激发,请将此添加到add_action()
电话:var_dump( did_action( \'admin_init\' ) );
要解决此问题,可以在菜单/子菜单类中创建静态方法/函数,并从Init_test
构造函数。的示例Init_Test_Home
类别:// In class-init-test.php
/* This is a simplified example! You\'d need to adjust the `Init_Test_Home` class
e.g. do `public static function save()` instead of `public function save()`,
and you should actually load the class prior to calling the `add_action()`; or
perhaps in init-test.php.
*/
public function __construct(){
add_action( \'admin_init\', array( Init_Test_Home, \'save\' ) );
...
}
或者您可以尝试我之前建议的代码/方法。:)但在这里,它略有不同,并且基于您现有的代码;这一次,再一次,是为了Init_Test_Home
类,但可以对子菜单类执行相同的步骤:输入class-init-test.php
:
public function add_menu() {
// Load and instantiate the class - $init_test_home is defined in the class file.
include INIT_TEST_DIR . \'/inc/admin/class-init-test-home.php\';
add_menu_page(
\'Init Test\',
\'Init Test\',
\'manage_options\',
\'init-test-home\', // Use a slug other than the class file path.
array( $init_test_home, \'admin_page\' ),
\'dashicons-yes\',
89
);
...
}
英寸class-init-test-home.php
:public function __construct() {
add_action( \'admin_init\', array( $this, \'save\' ) );
}
public function admin_page() {
require_once(\'views/init-test-home.php\');
}
定义INIT_TEST_DIR
像这样:define( \'INIT_TEST_DIR\', plugin_dir_path( __FILE__ ) );
希望这个答案更有帮助!