dpwu16132 2016-08-11 21:23
浏览 100
已采纳

admin_init没有被触发

I am coding a WordPress plugin where at first, I am adding Menus to WordPress and than each menu callback first triggers admin_init and than require desired page. Here is my Logic

<?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 is NOT getting triggered that is why next page is NOT loading. I have tried echo 1;exit; instead to require to check if the code flows to my echo 1; still it fails.

Here is my wp_easy_seo_title page :

<?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;


?>
  • 写回答

1条回答 默认 最新

  • duanmou9228 2016-08-12 03:39
    关注

    Right now, your call to add_action('admin_init'... is inside of a function wp_easy_seo_title_cb which does get called until sometime when menus are being rendered. This is after admin_init has already occurred, and so it is registered too late.

    Try moving ALL of your calls to add_action to the Constructor of your class.

    I find it good to register all actions in the constructor in order to keep things organized and ensure everything is registered in time.

    <?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();
    

    This way, when the file is loaded it immediately registers all of it's action hooks. Not much else should happen in the constructor other than registering hooks, because the constructor is going to run on EVERY page load.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况