我正在编写一个WordPress插件,首先,我将菜单添加到WordPress,并且每个菜单回调首先触发admin_init而不是需要所需的页面。这是我的逻辑
<?php
class EasySEO
{
public function __construct()
{
$this->wp_easy_seo_register_menu_page();
}
public function wp_easy_seo_register_menu_page()
{
add_menu_page('Easy SEO : General Setting', 'Easy SEO', 'manage_options', 'wp-easy-seo-dashboard', array(
$this,
'wp_easy_seo_general_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Easy SEO : General Setting', 'General', 'manage_options', 'wp-easy-seo-dashboard', array(
$this,
'wp_easy_seo_general_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Title & Metas - Easy SEO', 'Title & Metas', 'manage_options', 'wp_easy_seo_title', array(
$this,
'wp_easy_seo_title_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Verify Domain Ownership - Easy SEO', 'Domain Verification', 'manage_options', 'wp_easy_seo_domain_ownership_verify', array(
$this,
'wp_easy_seo_domain_ownership_verify_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Web Traffic Statistics - Easy SEO', 'Traffic Statistics', 'manage_options', 'wp_easy_seo_traffic_statistics', array(
$this,
'wp_easy_seo_traffic_statistics_cb'
));
}
function wp_easy_seo_general_cb()
{
}
function wp_easy_seo_title_cb()
{
add_action('admin_init', function()
{
require 'wp_easy_seo_title.php';
});
}
function wp_easy_seo_domain_ownership_verify_cb()
{
require 'wp_easy_seo_domain_ownership_verify.php';
}
function wp_easy_seo_traffic_statistics_cb()
{
require 'wp_easy_seo_traffic_statistics.php';
}
}
add_action('admin_menu', function()
{
new EasySEO;
});
?>
Admin_init未被触发,这就是下一页未加载的原因。我试过echo 1;退出;而是要求检查代码是否流向我的echo 1;它仍然失败。
这是我的wp_easy_seo_title页面:
<?php
class EasySEO_Options {
public $options;
public function __construct() {
$this->options= get_option('wp_easyseo_homepage_options');
$this->easy_seo_display_options_page();
$this->wp_easy_seo_register_settings_and_field();
}
public function easy_seo_display_options_page() {
?>
<div class='wrap'>
<h2>Title & Metas - Easy SEO</h2>
<form action="options.php" enctype="multipart/form-data" method="POST">
<?php
settings_fields('wp_easyseo_homepage_options');
do_settings_sections('wp_easy_seo_title');
?>
<p class="submit">
<input name='submit' type='submit' class='button-primary' value='Save Changes'/>
</p>
</form>
</div>
<?php
}
public function wp_easy_seo_register_settings_and_field() {
register_setting('wp_easyseo_homepage_options', 'wp_easyseo_homepage_options');
add_settings_section('wp-easyseo-main-settings', 'Title & Metas', array($this, 'wp_easyseo_main_settings_cb'), 'wp_easy_seo_title');
add_settings_field('wp-easy-seo-title', 'Meta Title', array($this, 'wp_easy_seo_title_cb'), 'wp_easy_seo_title', 'wp-easyseo-main-settings');
add_settings_field('wp-easy-seo-description', 'Meta Description', array($this, 'wp_easy_seo_description_cb'), 'wp_easy_seo_title', 'wp-easyseo-main-settings');
}
public function wp_easyseo_main_settings_cb() {
}
/**
* Inputs
*/
public function wp_easy_seo_title_cb() {
echo "<input name='wp_easyseo_homepage_options[wp-easy-seo-title]' value='{$this->options['wp-easy-seo-title']}' type='text'/>";
}
public function wp_easy_seo_description_cb() {
echo "<input name='wp_easyseo_homepage_options[wp-easy-seo-description]' value='{$this->options['wp-easy-seo-description']}' type='text'/>";
}
}
new EasySEO_Options;
?>
答案 0 :(得分:3)
现在,您对add_action('admin_init'...
的调用位于函数wp_easy_seo_title_cb
内部,该函数在呈现菜单时会被调用。这是在admin_init已经发生之后,因此注册太晚了。
尝试将您对add_action
的所有来电移至班级的构造函数。
我发现在构造函数中注册所有操作以保持组织有序并确保所有内容都及时注册。
<?php
class SomePlugin {
public function __construct() {
add_action('admin_init', array($this, 'some_callback'));
add_action('transition_post_status', array($this, 'other_callback'));
// etc
}
public function some_callback() {
// Do something
}
public function other_callback() {
// Do something
}
}
new SomePlugin();
这样,当文件加载时,它立即注册所有它的动作挂钩。除了注册钩子之外,构造函数中不会发生太多其他事情,因为构造函数将在每个页面加载时运行。