最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - Settings Fields not showing up in Submenu Page

matteradmin7PV0评论

I'm working on my first plugin. I am having difficulty getting the settings_fields function to display the results of the settings fields' callback functions. I've been studying the settings api, but I cannot figure out where I've gone wrong. I've attached a screenshot of what is rendering, as well as the code in question. Thanks for any help provided.

Result:

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();
}

I'm working on my first plugin. I am having difficulty getting the settings_fields function to display the results of the settings fields' callback functions. I've been studying the settings api, but I cannot figure out where I've gone wrong. I've attached a screenshot of what is rendering, as well as the code in question. Thanks for any help provided.

Result:

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();
}
Share Improve this question edited Apr 9, 2019 at 5:32 Karun 1,3471 gold badge9 silver badges22 bronze badges asked Apr 8, 2019 at 23:16 Doug L.Doug L. 1
Add a comment  | 

1 Answer 1

Reset to default 0

The the code below:

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();
}
Post a comment

comment list (0)

  1. No comments so far