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.

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

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM