# Wordpress plugin form not saving data

I am learning development of plugins, I am stuck into saving the plugin options form data.

I have a plugin options page, where three fields asking for number of videos, height and width are coded.

When I enter the values into it, and hit on save its just saving one value, that is, number of videos.

Here is my code

    <?php

// Init plugin options to white list our options
function ozh_sampleoptions_init(){
register_setting( 'ozh_sampleoptions_options', 'ozh_sample', 'ozh_sampleoptions_validate' );
}

}

// Draw the menu page itself
function ozh_sampleoptions_do_page() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields('ozh_sampleoptions_options'); ?>
<?php options = get_option('ozh_sample'); ?> <table class="form-table"> <tr valign="top"><th scope="row">No of videos:</th> <td><input type="text" name="ozh_sample[sometext]" value="<?php echooptions['sometext']; ?>" /></td>
</tr>

<tr valign="top"><th scope="row">Height:</th>
<td><input type="text" name="ozh_sample[hgt]" value="<?php echo options['hgt']; ?>" /></td> </tr> <tr valign="top"><th scope="row">Width:</th> <td><input type="text" name="ozh_sample[wid]" value="<?php echooptions['wid']; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php

}

// Sanitize and validate input. Accepts an array, return a sanitized array.
function ozh_sampleoptions_validate($input) { // Our first value is either 0 or 1 //$input['option1'] = ( $input['option1'] == 1 ? 1 : 0 ); // Say our second option must be safe text with no HTML tags$input['sometext'] =  wp_filter_nohtml_kses($input['sometext']);$input['hgt'] =  wp_filter_nohtml_kses($input['hgt']);$input['wid'] =  wp_filter_nohtml_kses($input['wid']); return$input;
}
$myoptions = get_option( 'ozh_sampleoptions_options' ); echo 'Niraj'; echo$options['sometext'];

?>


Its not saving height and width.

I know I have to work on <input type='hidden' name='page_options' value='vidNO'/> code, but not getting it,

Can anyone help me in this???

-

First, you really should be storing your options as an array in wp_options. But should you choose not to, you really should change the names of your second and third options; "height" and "width" are entirely too generic, and almost assuredly will cause conflicts. You're passing name="height" and name="width", respectively, but I doubt that WordPress is associating "width" and "height" as options that belong to your Plugin.

So, assuming your store your options as plugin_plugin-slug_options, which is an array:

<?php
$plugin_options = get_option( 'plugin_plugin-slug_options' ); ?> <tr><td>Number Of Videos:</td><td><input type="text" name="$plugin_options[vidNO]"  value="<?php echo get_option('vidNO');?>" <?php echo get_option('vidNO'); ?> />
</td></tr>

<tr><td>Height:</td><td><input type="text"  name="$plugin_options[height]" value="<?php echo get_option('height');?>" <?php echo get_option('height'); ?> /> </td></tr> <tr><td>Width:</td><td><input type="text" name="$plugin_options[width]" value="<?php echo get_option('width');?>" <?php echo get_option('width'); ?> />
</td></tr>


But you really should consider using the Settings API, at least insofar as using register_setting() to register your options array, and settings_fields() to do the heavy lifting in your settings form.

-
@Chip Bennett When I copy and paste your code, no settings get saved? –  ntechi May 20 '11 at 17:06
Creating settings for Plugins and Themes is not a matter of cut and paste. You need to understand what you're doing. If you were to use the Settings API/register_setting(), I could probably help walk you through the process. (Or, start by reading the tutorial.) –  Chip Bennett May 20 '11 at 18:32
@Chip Bennett I understood this method planetozh.com/blog/2009/05/… But after implementing this, I am stuck to get the value, I mean previously I used to get the saved field like get_options('name') but in this its not working –  ntechi May 21 '11 at 4:07
@ntechi you would use $myoptions = get_option( 'plugin_plugin-slug_options' ), and then to use/output any particular option, simply call $myoptions['name']. –  Chip Bennett May 21 '11 at 14:36
@Chip Bennett I tried $myoptions = get_option( 'plugin_plugin-slug_options' ); echo$myoptions['video']; and $myoptions = get_option( 'plugin_plugin-slug_options' );$myoptions['video']; but both are not working –  ntechi May 21 '11 at 17:48

WordPress will do the work for you.

Quick example of an option page containing a form with 1 field.

add_action('admin_menu', 'c3m_myplugin_add_page');
}

// Draw the option page
function c3m_myplugin_option_page() {
?>
<div class="wrap">
<form action="options.php" method="post">
<?php settings_fields( 'c3m_myplugin_options' ); ?>
<?php do_settings_sections( 'c3m_myplugin' ); ?>
<input name="Submit" type="submit" value="Save Changes" />
</form>
</div>
<?php
}

// Register and define the settings
register_setting(
'c3m_myplugin_options',
'c3m_myplugin_options'
);
'c3m_myplugin_main',
'c3m_myplugin_section_text',
'c3m_myplugin'
);
'c3m_myplugin_text_string',
'Enter text here',
'c3m_myplugin_setting_input',
'c3m_myplugin',
'c3m_myplugin_main'
);
}

function c3m_myplugin_section_text() {

I followed this, and it really helped me, its saving the settings, but how do I call the saved values? normally I used to call like get_options('name') but this is not working in this –  ntechi May 20 '11 at 17:32
get_option('name'); should work exactly the same. You have the function misspelled in your comment, so be sure to check that. –  Matthew Muro May 23 '11 at 15:24