Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am going to create a wordpress plugin with a option page and I have created a two page called index.php and options.php and in index.php I have written the following code to create default options value and to add or update option.

<?php
    function sis_stt_check_WP_ver(){
        $options_array = array(
            'youtube_username' => 'sayful', 
            'account_type' => 'none', 
            'facebook_username' => 'anik', 
        );
        if ( get_option( 'sis_stt_options' ) !== false ) {
            update_option( 'sis_stt_options', $options_array );
        }else{
            add_option( 'sis_stt_options', $options_array );
        }
    }
    register_activation_hook( __FILE__, 'sis_stt_check_WP_ver' );

?>

I also write the following code to registers our options to be updated and to add an admin page.

<?php
    // This function registers our options to be updated.
    add_action('admin_init', 'sis_stt_register_settings');

    function sis_stt_register_settings(){

        register_setting( 'sis_stt_plugin_options', 'sis_stt_options','sis_stt_validate_data');

    }

    // add the admin options page

    add_action('admin_menu', 'sis_stt_add_menu');

    function sis_stt_add_menu(){

       add_options_page( 'sis stt demo', 'sis stt demo', 'manage_options', 'plugin-folder/options.php');

    }

?>

In options.php file I have written the following code to display my option. When I install plugin it show default data in option page but when I change data only the first two fields data save but the data selected from select field does not save at all. What can I do and how can I fix this bug. Help me someone please.

<div class="wrap">
<h2>WordPress Standard Coding</h2>
<form method="post" action="options.php">
    <?php $options = get_option( 'sis_stt_options' ); ?>

    <?php settings_fields('sis_stt_plugin_options'); ?>
    <?php do_settings_sections( 'sis_stt_plugin_options' ); ?>

    <table class="form-table">
    <tbody>
      <tr valign="top">
        <th scope="row">
          <label><?php _e('YouTube Username') ?></label>
        </th>
        <td>
          <input type="text" name="sis_stt_options[youtube_username]" id="youtube_username_input" value="<?php esc_attr_e($options['youtube_username']); ?>" class="" placeholder="Scroll to top">
        </td>
      </tr>
      <tr valign="top">
        <th scope="row">
          <label><?php _e('Facebook Username') ?></label>
        </th>
        <td>
          <input type="text" name="sis_stt_options[facebook_username]" id="facebook_username" value="<?php esc_attr_e($options['facebook_username']); ?>" class="" placeholder="Scroll to top">
        </td>
      </tr>
      <tr valign="top">
        <th scope="row">
          <label><?php _e('Select Account Type') ?></label>
        </th>
        <td>
            <select name="sis_stt_options[account_type]">
               <option value="slide" <?php selected( $options['account_type'], 'slide' ); ?>>Slide</option>
               <option value="social" <?php selected( $options['account_type'], 'social' ); ?>>Social</option>
               <option value="none" <?php selected( $options['account_type'], 'none' ); ?>>None</option>
           </select>
        </td>
      </tr>
      </tbody>
      </table>
     <p class="submit"><input type="submit" value="<?php _e('Save Changes') ?>" class="button button-primary" id="submit" name="submit"></p>
  </form>

share|improve this question
    
Are you sure its not saved correctly? Try print_r on the $options variable.Maybe the selected() function is not working properly. –  passatgt May 1 at 16:58
1  
Maybe you need to use add_settings_field() to add each fields to your optios page. Also note that you are defining the function sis_stt_validate_data() in register_setting() for validation callback but that function is not in your code and this can lead to a PHP fatal error. –  cybmeta May 1 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.