为什么注册设置()不能创建一个设置?

时间:2014-04-25 作者:Pete

我被难住了。我一直在疯狂地研究这个,但我似乎不明白为什么register_setting() 对我没用。我是WP插件开发的新手(很明显),所以我可能错过了一些愚蠢的东西。看看吧。

我正在构造函数中运行此函数:

add_action( \'admin_init\', array( $this, \'register_settings_and_fields\') );
然后,我有以下代码可以执行我需要的内容:

/**
 * Register the settings and fields for the plugin settings page.
 *
 * @since    1.0.0
 */
public function register_settings_and_fields() {

    register_setting(\'my_plugin_settings\', \'my_plugin_settings\');

    add_settings_section(
        \'my_plugin_main_section\',                          // ID used to identify this section and with which to register options
        \'Main Settings\',                                   // Title to be displayed on the administration page
        array($this, \'my_plugin_main_settings_callback\'),  // Callback used to render the description of the section
        $this->plugin_slug                                 // Page on which to add this section of options
    );

    // Begin defining the fields
    add_settings_field(\'my_plugin_data_1\', \'Data 1\', array( $this, \'my_plugin_data_1_setting\' ), $this->plugin_slug, \'my_plugin_main_section\');
}

// ... and of course other lines of code.
当然,我有更多的代码来执行诸如定义回调函数等操作。一切似乎都执行得很好,设置部分/字段显示正确。但当我去/wp-admin/options.php, 尚未创建my\\u plugin\\u设置。

我被难住了。有什么想法吗?如果您想查看更多代码,请告诉我。

2 个回复
最合适的回答,由SO网友:Nick Young 整理而成

我不太清楚您所说的可以正确显示设置部分和字段是什么意思,但您的my\\u plugin\\u设置尚未创建。

我知道有时我忘记添加实际的管理部分,其中通常看起来像这样:

settings_fields( \'my_plugin_settings\' );
do_settings_sections( \'my_plugin_settings\' );

submit_button();
或者您在保存表单时遇到问题?如果是这种情况,那么我需要查看您的回调函数。

抱歉,如果这不准确,你需要什么,但我可以;在发布答案之前,请不要对您的问题发表评论,以进一步澄清。如果没有答案,请告诉我,我会相应地调整我的答案。

编辑#1:

我会诚实地说,我不能百分之百确定这些设置是否会自动添加到数据库中。在添加设置部分和设置字段之前,我要做的是这样。

/* If the options do not exist then create them*/
if ( false == get_option( \'my_plugin_settings\' ) ) {
    add_option( \'my_plugin_settings\' );
}
这将在数据库中创建选项。

SO网友:Juan

在我的例子中,是什么解决了调试选项后的问题。php关于$whitelist\\u options变量$option_group 参数与选项页slug相同。

在您的示例中

register_setting($this->plugin_slug, \'my_plugin_settings\');

add_settings_section(
        \'my_plugin_main_section\',                          // ID used to identify this section and with which to register options
        \'Main Settings\',                                   // Title to be displayed on the administration page
        array($this, \'my_plugin_main_settings_callback\'),  // Callback used to render the description of the section
        $this->plugin_slug                                 // Page on which to add this section of options
    );

结束

相关推荐

如何创建多个顶级管理菜单页面以将数据提交到Options表?

我试图在wordpress主题中创建多个顶级选项页面。但现在我只能保存一个的数据。如何在不必创建选项卡式面板的情况下为两个人保存数据?// create custom plugin settings menu add_action(\'admin_menu\', \'baw_create_menu\'); function baw_create_menu() { //create new top-level menu add_menu_page(\'B