子菜单页面中未显示的设置字段

时间:2019-04-08 作者:Doug L.

我正在开发我的第一个插件。我很难让settings\\u fields函数显示设置字段回调函数的结果。我一直在研究设置api,但我不知道哪里出了问题。我附上了渲染内容的屏幕截图,以及相关代码。感谢您提供的任何帮助。

结果:the results of do_settings_sections shows up, but not settings_fields

if(!class_exists(\'ce_add_scripts\')){

    class ce_add_scripts{

        function __construct(){
            add_action(\'admin_init\', array(&$this,\'ceas_admin_init\'));
            add_action(\'admin_menu\', array(&$this,\'ceas_admin_menu\'));
            add_action(\'wp_head\', array(&$this,\'ceas_wp_head\'));
            add_action(\'wp_footer\', array(&$this,\'ceas_wp_footer\'));
        }

        function ceas_admin_menu(){
            add_submenu_page(
                \'tools.php\', 
                \'CE Add Scripts\', 
                \'CE Add Scripts\', 
                \'update_plugins\', 
                \'ceas_menu_page\', 
                array(&$this,\'ceas_page_callback\') );
        }

        function ceas_admin_init(){
            $args= array(
                \'type\'=>\'string\',
                \'default\'=>NULL,);

            register_setting(
                \'ceas_options\', 
                \'ceas_main_settings\', 
                $args);

            add_settings_section(
                \'ceas_options_section\',
                \'Insert Scripts to Head or Footer\',
                array(&$this,\'ceas_section_display\'),
                \'ceas_menu_page\');

            add_settings_field(
                \'ceas_options_field_0\',
                \'Head Script Entry\',
                \'ceas_head_script\',
                \'CE Add Scripts\',
                \'ceas_options_section\');

            add_settings_field(
                \'ceas_options_field_1\',
                \'Footer Script Entry\',
                \'ceas_footer_script\',
                \'CE Add Scripts\',
                \'ceas_options_section\');
        }

        function ceas_section_display(){
            echo \'Add scripts to the Head or Footer\';
        }

        function ceas_head_script(){
            $options = get_option(\'ceas_main_settings\');

            echo "<textarea cols=\'40\' rows=\'5\' name=\'ceas_main_settings[ceas_options_field_0]\'>";
            echo $options[\'ceas_options_field_0\'];
            echo "</textarea>";
        }

        function ceas_footer_script(){
            $options = get_option(\'ceas_main_settings\');

            echo "<textarea cols=\'40\' rows=\'5\' name=\'ceas_main_settings[ceas_options_field_1]\'>";
            echo $options[\'ceas_options_field_1\'];
            echo "</textarea>";
        }

        function ceas_page_callback(){

            echo "<div class=\'wrap\'>

            <form action=\'options.php\' method=\'post\'>

            <h1>CE Add Scripts Settings</h1>";

            settings_fields("ceas_options");
            do_settings_sections(\'ceas_menu_page\');
            submit_button();


            echo "</form> 
            </div>";
        }

    }

    $ce_actualize= new ce_add_scripts();
}

1 个回复
SO网友:Karun

代码如下:

if(!class_exists(\'ce_add_scripts\')){

    class ce_add_scripts{

        function __construct(){
            add_action(\'admin_init\', array(&$this,\'ceas_admin_init\'));
            add_action(\'admin_menu\', array(&$this,\'ceas_admin_menu\'));
            add_action(\'wp_head\', array(&$this,\'ceas_wp_head\'));
            add_action(\'wp_footer\', array(&$this,\'ceas_wp_footer\'));
        }

        function ceas_admin_menu(){
            add_submenu_page(
                \'tools.php\', 
                \'CE Add Scripts\', 
                \'CE Add Scripts\', 
                \'update_plugins\', 
                \'ceas_menu_page\', 
                array(&$this,\'ceas_page_callback\') );
        }

        function ceas_admin_init(){
            $args= array(
                \'type\'=>\'string\',
                \'default\'=>NULL,);

            register_setting(
                \'ceas_options\', 
                \'ceas_main_settings\', 
                $args);

            add_settings_section(
                \'ceas_options_section\',
                \'Insert Scripts to Head or Footer\',
                array(&$this,\'ceas_section_display\'),
                \'ceas_menu_page\');

            add_settings_field(
                \'ceas_options_field_0\',
                \'Head Script Entry\',
                \'ceas_head_script\',
                \'CE Add Scripts\',
                \'ceas_options_section\');

            add_settings_field(
                \'ceas_options_field_1\',
                \'Footer Script Entry\',
                \'ceas_footer_script\',
                \'CE Add Scripts\',
                \'ceas_options_section\');
        }

        function ceas_section_display(){
            echo \'Add scripts to the Head or Footer\';
        }

        function ceas_head_script(){
            $options = get_option(\'ceas_main_settings\');
            if(!isset($options[\'ceas_options_field_0\'])){
                $options[\'ceas_options_field_0\'] = \'\';
            }
            echo "<textarea cols=\'40\' rows=\'5\' name=\'ceas_main_settings[ceas_options_field_0]\'>";
            echo $options[\'ceas_options_field_0\'];
            echo "</textarea>";
        }

        function ceas_footer_script(){
            $options = get_option(\'ceas_main_settings\');
            if(!isset($options[\'ceas_options_field_1\'])){
                $options[\'ceas_options_field_1\'] = \'\';
            }
            echo "<textarea cols=\'40\' rows=\'5\' name=\'ceas_main_settings[ceas_options_field_1]\'>";
            echo $options[\'ceas_options_field_1\'];
            echo "</textarea>";
        }

        function ceas_page_callback(){

            echo "<div class=\'wrap\'>

            <form action=\'options.php\' method=\'post\'>

            <h1>CE Add Scripts Settings</h1>";

            settings_fields("ceas_options");
            do_settings_sections(\'ceas_menu_page\');
            $this->ceas_head_script();
            $this->ceas_footer_script();
            submit_button();


            echo "</form> 
            </div>";
        }

    }

    $ce_actualize= new ce_add_scripts();
}

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。