0

Here is my option page code containing a no of fields like logo, slider after filling all the information in option page form. I need two things

  1. After submitting all the form details save information must retain there.

  2. Using get_option() extract each input tag value and show that value in the front hand like slider image, slider heading, slider description

OPTION PAGE CODE:

    <?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>';

    }?>

Same as for slider image, slider heading, slider description. Please suggest some solutions

  • Is this a wordpress.com hosted site. If so, your question is off topic and should be asked on their forum. If not, please remove the wordpress.com tag and properly format your question – Pieter Goosen Jun 10 '14 at 7:33
0

I think your issue is in this section here:

     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' );

What I would do to change it is something like this:

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

The way you are setting your options with $this->options = right now is not adding to it each time since it is not using .= to append it. If you add them all like in my example it will be combined into one big array that is accessible with $this->options

Note: I wrote this code out but did not actually test it. There could be other problems but this is where I believe most of it lies.

Hope this helps.

  • thanks nick for your suggestions still get_option not echo any value – Templategraphy Jun 10 '14 at 8:30
  • I just realized something. Is all of this code in the same file? It might actually be a hook timing issue. – Nick Young Jun 10 '14 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.