dpwu16132 2016-08-11 13: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-11 19: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 使用X11可以找到托盘句柄,监控到窗口点击事件但是如何在监听的同时获取托盘中应用的上下文菜单句柄
  • ¥15 IEd中开关量采样信号通道设计
  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部