OOP插件和菜单-调用未定义的函数REGISTING_SETTING()

时间:2012-11-13 作者:s6712

WordPress文档在面向对象的代码上似乎参差不齐。我正在设置一些插件,第一个插件应该注册设置和菜单页。它为register_setting()add_settings_field() 呼叫。这些应该去其他地方吗?

我也没有看到管理设置页面被创建?谢谢你的帮助。

<?php
//$c = new mySettings();
class mySettings {

    public function __construct() {
        $this->add_hooks();
        $this->register_settings();
    }
    public function add_hooks() {
        add_action(\'admin_menu\', array($this, \'admin_menu\'));
        add_action(\'admin_init\', array($this, \'register_settings\'));
    }
    public function register_settings() {
        //exit(\'register_settings\'. microtime());
        register_setting(\'mySettings\', \'mySettings\', null);
        add_settings_field(\'myUrl\', \'API URL\', array($this, \'myUrl_callback\'), \'my\');
        add_settings_field(\'myOauthUrl\', \'API Host\', array($this, \'myOauthUrl_callback\'), \'my\');
        add_settings_field(\'myClientId\', \'API Client Id\', array($this, \'myClientId_callback\'), \'my\');
        add_settings_field(\'mySharedSecret\', \'API Shared Secret\', array($this, \'mySharedSecret_callback\'), \'my\');
    }
    public function admin_menu() {
        exit(\'admin_menu\'. mircotime());
        add_options_page(\'my API Settings\', \'my Settings\', \'manage_options\', \'my-my\', array($this, \'settings_page\'));
    }
    public function settings_page() {
        if (!current_user_can(\'manage_options\')) {
            wp_die(__(\'You do not have sufficient permissions to access this page.\'));
        }
        print \'<h2>my API Settings</h2><form action="options.php" method="post">\';
        print settings_fields(\'mySettings\');
        print do_settings_sections(\'mySettings_selections\');
        print submit_button() .\'</form>\';
    }
    public function myUrl_callback() {
        $options = get_option(\'mySettings\');
        $option = isset($options[\'myUrl\']) ? $options[\'myUrl\'] : \'\';
        print \'<input id="myUrl" name="mySettings[myUrl]" type="text" value="\' . $option . \'" />\';
    }

}

1 个回复
SO网友:chrisguitarguy

它们是未定义的,因为加载插件(或主题)时函数不存在——管理区域包含的功能尚未出现。

如果要注册设置字段,最好连接到admin_init 这样做。

简而言之,您可以通过以下操作修复类:

<?php
class mySettings {

    public function __construct() {
        $this->add_hooks();
    }

    // other stuff here
}
或者这个:

<?php
//$c = new mySettings();
class mySettings {

    public function __construct() {
        add_action(\'admin_menu\', array($this, \'admin_menu\'));
        add_action(\'admin_init\', array($this, \'register_settings\'));
    }

    // other stuff here
}
它只是关于文件在WordPress中被包含的顺序。插件和主题很早就被加载,WP甚至还不知道它要去哪里。这就是为什么你稍后会加入(比如admin_initinit) 执行诸如注册设置或帖子类型之类的操作。

另外,我喜欢这样构造类:

<?php
class WPT26_Awesome
{
    private static $ins = null;

    public static function init()
    {
        // for themes: add_action(\'after_setup_theme\', array(self::instance(), \'_setup\'));
        add_action(\'plugins_loaded\', array(self::instance(), \'_setup\'));
    }

    public static function instance()
    {
        // create a new object if it doesn\'t exist.
        is_null(self::$ins) && self::$ins = new self;
        return self::$ins;
    }

    public function _setup()
    {
        // other calls to `add_action` here.
    }
}
将操作/过滤器排除在构造函数之外,这样可以使测试更加容易。你用…开始一切。。。WPT26_Awesome::init();. 这里有一个tutorial 关于这个问题。

结束

相关推荐