回显选项页面表单字段值

时间:2014-06-10 作者:Templategraphy

这是我的选项页面代码,在填写完选项页面表单中的所有信息后,它包含了一些字段,如徽标、滑块。我需要两样东西

提交所有表单详细信息后,保存信息必须保留在那里。

使用get_option() 提取每个输入标记值,并将该值显示在前面的手状滑块图像、滑块标题、滑块描述中

选项页代码:

    <?php
    class MySettingsPage
    {
    /**
         * Holds the values to be used in the fields callbacks
         */
        private $options;

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


        }

             public function bguru_register_options_page()
        {
            // This page will be under "Settings"
         add_theme_page(\'Business Guru Options\',
                         \'Theme Customizer\', 
                         \'edit_theme_options\', 
                         \'bguru-options\', 
                         array( $this, \'bguru_options_page\')
            );
        }

    public function bguru_options_page()
        {
            // Set class property
             $this->options = get_option( \'bguru_logo\' );
             $this->options = get_option( \'bguru_vimeo\' );
             $this->options = get_option( \'bguru_slide_one_image\' );
             $this->options = get_option( \'bguru_slide_one_heading\' );
             $this->options = get_option( \'bguru_slide_one_text\' );
             $this->options = get_option( \'bguru_slogan_heading\' );
             $this->options = get_option( \'bguru_slogan_description\' );


            ?>
            <div class="wrap">
                <?php screen_icon(); ?>
              <h1>Business Guru Options</h1>      
                <form method="post" action="options.php">
                    <table class="form-table">
                <?php
                    // This prints out all hidden setting fields
                    settings_fields( \'defaultbg\' );   
                    do_settings_sections( \'defaultbg\' );
                    submit_button(); 
                ?>
                    </table>
                </form>
            </div>
            <?php
        }

     /**
         * Register and add settings
         */
        public function bguru_register_settings()
        {        
    register_setting(\'defaultbg\',\'bguru_logo\',  array( $this, \'sanitize\' ) );
    register_setting(\'defaultbg\', \'bguru_vimeo\',  array( $this, \'sanitize\' ));
    register_setting(\'defaultbg\', \'bguru_slide_one_image\',  array( $this, \'sanitize\' ));
    register_setting(\'defaultbg\', \'bguru_slide_one_heading\',  array( $this, \'sanitize\' ));
    register_setting(\'defaultbg\', \'bguru_slide_one_text\',  array( $this, \'sanitize\' ));
    register_setting(\'defaultbg\', \'bguru_slogan_heading\',  array( $this, \'sanitize\' ));
    register_setting(\'defaultbg\', \'bguru_slogan_description\',  array( $this, \'sanitize\' ));
           add_settings_section(
                \'setting_section_id\', // ID
                \'<h2>General</h2>\', 
                array( $this, \'print_section_info\' ), // Callback
                \'defaultbg\' // Page

            );  

            add_settings_field( 

                \'bguru_logo\', // ID
                \'<label for="bguru_logo">Logo</label>\', // Title 
                array($this,\'logo_callback\' ), // Callback
                 \'defaultbg\', // Page
               \'setting_section_id\'// Section       

            );      


            add_settings_field(
                \'bguru_vimeo\', // ID
                \'Vimeo\', // Vimeo 
                array( $this, \'socialv_callback\' ), // Callback
                 \'defaultbg\', // Page
                \'setting_section_id\' // Section           
            );

            add_settings_field(
                \'bguru_slide_one_image\', // ID
                \'Slide 1 Image\', // Slide 1 Image 
                array( $this, \'slider1img_callback\' ), // Callback
                 \'defaultbg\', // Page
                \'setting_section_id\' // Section           
            );

            add_settings_field(
                \'bguru_slide_one_heading\', // ID
                \'Slide 1 Heading\', // Slide 1 Heading 
                array( $this, \'slider1head_callback\' ), // Callback
                 \'defaultbg\', // Page
                \'setting_section_id\' // Section           
            );

            add_settings_field(
                \'bguru_slide_one_text\', // ID
                \'Slide 1 Description\', // Slide 1 Description
                array( $this, \'slider1text_callback\' ), // Callback
                 \'defaultbg\', // Page
                \'setting_section_id\' // Section           
            );

            add_settings_field(
                \'bguru_slogan_heading\', // ID
                \'Slogan Heading\', // Slogan Heading
                array( $this, \'slogan_head_callback\' ), // Callback
                 \'defaultbg\', // Page
                \'setting_section_id\' // Section           
            );

            add_settings_field(
                \'bguru_slogan_description\', // ID
                \'Slogan Container\', // Slogan Container
                array( $this, \'slogan_descr_callback\' ), // Callback
                 \'defaultbg\', // Page
                \'setting_section_id\' // Section           
            );

        }


     public function sanitize( $input )
        {
            $new_input = array();


                if( isset( $input[\'bguru_logo\'] ) )
                $new_input[\'bguru_logo\'] = sanitize_text_field( $input[\'bguru_logo\'] );

                 if( isset( $input[\'bguru_vimeo\'] ) )
                $new_input[\'bguru_vimeo\'] = sanitize_text_field( $input[\'bguru_vimeo\'] );

                 if( isset( $input[\'bguru_slide_one_image\'] ) )
                $new_input[\'bguru_slide_one_image\'] = sanitize_text_field( $input[\'bguru_slide_one_image\'] );

                 if( isset( $input[\'bguru_slide_one_heading\'] ) )
                $new_input[\'bguru_slide_one_heading\'] = sanitize_text_field( $input[\'bguru_slide_one_heading\'] ); 

                if( isset( $input[\'bguru_slide_one_text\'] ) )
                $new_input[\'bguru_slide_one_text\'] = sanitize_text_field( $input[\'bguru_slide_one_text\'] );

                if( isset( $input[\'bguru_slogan_heading\'] ) )
                $new_input[\'bguru_slogan_heading\'] = sanitize_text_field( $input[\'bguru_slogan_heading\'] );

                 if( isset( $input[\'bguru_slogan_description\'] ) )
                $new_input[\'bguru_slogan_description\'] = sanitize_text_field( $input[\'bguru_slogan_description\'] );

                 return $new_input;
        }
    public function print_section_info()
        {
            print \'Enter your settings below:\';
        }

    public function logo_callback()
        {

            printf(
                \'<input type="text" id="bguru_logo"  size="50" name="bguru_logo" value="%s" />\',
                isset( $this->options[\'bguru_logo\'] ) ? esc_attr( $this->options[\'bguru_logo\']) : \'\'
            );
        }

    public function socialv_callback()
        {
            printf(
                \'<input type="text" id="bguru_vimeo"  size="50" name="bguru_vimeo" value="%s" />\',
                isset( $this->options[\'bguru_vimeo\'] ) ? esc_attr( $this->options[\'bguru_vimeo\']) : \'\'
            );
        }


        public function slider1img_callback()
        {
            printf(
                \'<input type="text" id="bguru_slide_one_image"  size="50" name="bguru_slide_one_image" value="%s" />\',
                isset( $this->options[\'bguru_slide_one_image\'] ) ? esc_attr( $this->options[\'bguru_slide_one_image\']) : \'\'
            );
        }
        public function  slider1head_callback()
        {
            printf(
                \'<input type="text" id="bguru_slide_one_heading"  size="50" name="bguru_slide_one_heading" value="%s" />\',
                isset( $this->options[\'bguru_slide_one_heading\'] ) ? esc_attr( $this->options[\'bguru_slide_one_heading\']) : \'\'
            );
        }
        public function slider1text_callback()
        {
            printf(
                \'<input type="text" id="bguru_slide_one_text"  size="50" name="bguru_slide_one_text" value="%s" />\',
                isset( $this->options[\'bguru_slide_one_text\'] ) ? esc_attr( $this->options[\'bguru_slide_one_text\']) : \'\'
            );
        }


        public function slogan_head_callback()
        {
            printf(
                \'<input type="text" id="bguru_slogan_heading"  size="50" name="bguru_slogan_heading" value="%s" />\',
                isset( $this->options[\'bguru_slogan_heading\'] ) ? esc_attr( $this->options[\'bguru_slogan_heading\']) : \'\'
            );
        }

        public function slogan_descr_callback()
        {
            printf(
                \'<input type="text" id="bguru_slogan_description"  size="50" name="bguru_slogan_description" value="%s" />\',
                isset( $this->options[\'bguru_slogan_description\'] ) ? esc_attr( $this->options[\'bguru_slogan_description\']) : \'\'
            );
        }

    }
    if( is_admin() )
        $my_settings_page = new MySettingsPage();

here its my header.php code where i display all the information of  option form

<?php           
    $bguru_logo_image = get_option(\'bguru_logo\');

    if (!empty($bguru_logo_image)) {

        echo \'<div id="logo"><a href="\' . home_url() . \'"><img src="\' . $bguru_logo_image . \'" width="218" alt="logo" /></a></div><!--/ #logo-->\';

    } else {

        echo \'<div id="logo"><a href="\' . home_url() . \'"><h1>\'. get_bloginfo(\'name\') . \'</h1></a></div><!--/ #logo-->\';

    }?>
                    <?php

    $bguru_social_vimeo = get_option(\'bguru_vimeo\');
if (!empty($bguru_social_vimeo)) {

        echo \'<li class="vimeo"><a target="_blank" href="\'.$bguru_social_vimeo.\'">Vimeo</a></li>\';

    }?>
与滑块图像、滑块标题、滑块描述相同。请提出一些解决方案

1 个回复
SO网友:Nick Young

我想你的问题就在这一部分:

     public function bguru_options_page()
    {
        // Set class property
         $this->options = get_option( \'bguru_logo\' );
         $this->options = get_option( \'bguru_vimeo\' );
         $this->options = get_option( \'bguru_slide_one_image\' );
         $this->options = get_option( \'bguru_slide_one_heading\' );
         $this->options = get_option( \'bguru_slide_one_text\' );
         $this->options = get_option( \'bguru_slogan_heading\' );
         $this->options = get_option( \'bguru_slogan_description\' );
我想做些什么来改变它是这样的:

     public function bguru_options_page()
    {
        $bguru_logo = is_array( get_option( \'bguru_logo\' ) ) ? get_option( \'bguru_logo\' ) : array();
         $bguru_vimeo = is_array( get_option( \'bguru_vimeo\' ) ) ? get_option( \'bguru_vimeo\' ) : array();
         $bguru_slide_one_image = is_array( get_option( \'bguru_slide_one_image\' ) ) ? get_option( \'bguru_slide_one_image\' ) : array();
         $bguru_slide_one_heading = is_array( get_option( \'bguru_slide_one_heading\' ) ) ? get_option( \'bguru_slide_one_heading\' ) : array();
         $bguru_slide_one_text = is_array( get_option( \'bguru_slide_one_text\' ) ) ? get_option( \'bguru_slide_one_text\' ) : array();
         $bguru_slogan_heading = is_array( get_option( \'bguru_slogan_heading\' ) ) ? get_option( \'bguru_slogan_heading\' ) : array();
         $bguru_slogan_description = is_array( get_option( \'bguru_slogan_description\' ) ) ? get_option( \'bguru_slogan_description\' ) : array();

         $this->options = array_merge( $bguru_logo, $bguru_vimeo, $bguru_slide_one_image, $bguru_slide_one_heading, $bguru_slide_one_text, $bguru_slogan_heading, $bguru_slogan_description );
设置选项的方式$this->options = 因为它没有使用.= 添加它。如果您像我的示例中那样添加它们,它将组合成一个大数组,可以使用$this->options

注意:我编写了这段代码,但实际上并没有对其进行测试。可能还有其他问题,但我相信这就是大部分问题所在。

希望这有帮助。

结束

相关推荐

更新复杂`wp_options`对象中的特定键值

这应该很容易,但我找不到任何人的参考点!我有一个选择wp_options 表,需要通过update_option(). 我只是找不到正确的语法来执行类似以下更新密钥的操作object_key 在选项中my_plugin_settings 在我的wp_options 表格: update_option(\'my_plugin_settings[object_key]\',\'new value\'); 如何正确完成?