尝试保存自定义POST类型设置时出错(错误:未找到选项页面)

时间:2014-12-16 作者:mrberggg

我知道这是一个常见的错误,但我还没有遇到任何行之有效的解决方案。我正在创建一个使用自定义帖子类型的插件,并且在CPT菜单下有一个设置页面。设置页面显示良好,但当我去保存设置时,会出现错误ERROR: options page not found. 这是我的设置类:

<?php namespace SAW\\Hours;

class Settings {
    private $view;

    public function __construct( $view ) {
        $this->view = $view;
        add_action( \'admin_menu\', array( $this, \'submenu\' ) );
        add_action( \'admin_init\', function () {
            add_settings_section(
                \'saw_hours\',
                \'Hours Settings\', // Title
                array( $this, \'settings\' ),
                \'saw_hours_settings\'
            );
        } );
    }

    public function submenu()
    {
        add_submenu_page(
            "edit.php?post_type=saw_hours", // Parent slug
            "Settings", // Page title
            "Settings", // Menu title
            "activate_plugins", // Role required
            \'saw_hours_settings\', // Menu slug
            array($this->view, \'admin\'));
    }

    public function settings()
    {
        // API key setting
        register_setting(
            \'saw_hours\',
            \'saw_hours_api_key\'
        );
        add_settings_field(
            \'saw_hours_api_key\',
            \'Set API Key:\',
            function(){
                $clientId = get_option(\'saw_hours_api_key\');
                echo \'<input type="text" name="saw_hours_api_key" value="\' . $clientId . \'" >\';
            },
            \'saw_hours_settings\',
            \'saw_hours\'
        );

        // Client ID setting
        register_setting(
            \'saw_hours\',
            \'saw_hours_client_id\'
        );
        add_settings_field(
            \'saw_hours_client_id\',
            \'Set Client ID:\',
            function(){
                $clientId = get_option(\'saw_hours_client_id\');
                echo \'<input type="text" name="saw_hours_client_id" value="\' . $clientId . \'" >\';
            },
            \'saw_hours_settings\',
            \'saw_hours\'
        );
    }


}
这是表单页:

<form method="POST" action="options.php">
    <?php settings_fields( \'saw_hours\' ); ?>
    <?php do_settings_sections( \'saw_hours_settings\'); ?>
    <?php submit_button(); ?>
</form>
我不知所措,任何帮助都将不胜感激。

1 个回复
SO网友:mrberggg

这起到了作用:

"Error: Options Page Not Found" on Settings Page Submission for an OOP Plugin

我想理解为什么register\\u设置没有正确地将属性添加到白名单中。

结束

相关推荐