First, I'd like to pre-emptively apologize for doing anything naive or foolish. I'm a novice programmer, but I'm trying to learn. Right now, I'm trying to develop a plugin for wordpress as part of my internship. The plugin does what it needs to do for the most part, but I just can't seem to make the settings page work the way I want. I know the initialization part of the plugin is working properly, but I'm having trouble with using the settings page. Specifically, I don't where or how to use _POST in order to save the information. I also don't where to place the update_option function call after I've received the information from the settings page. Here's my code right now:
add_action( 'admin_menu', 'menu' );
add_action( 'admin_init', 'plugin_admin_init' );
function plugin_admin_init() { // whitelist options
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
add_settings_field('plugin_text_string', 'REE View Count Settings', 'plugin_setting_string', 'plugin', 'plugin_main');
}
function menu() {
add_options_page( 'REE View Count Options', 'REE View Count', 'manage_options', 'plugin', 'plugin_options_page' );
}
// generates the settings web page
function plugin_options_page() {
echo '<div class="wrap">';
echo '<h2>Options</h2>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
// In the line above, I don't know what to put. The file name of the plugin?
// The URL of the settings page on the admin page? Or what I have currently?
// Both the first and last options bring up a 404 page.
settings_fields('plugin_options');
do_settings_sections('plugin');
echo '<input name="Submit" type="submit" value="' . esc_attr_e('Save Changes') . '" />';
echo '</form>';
//update_option('plugin_options', $_POST['plugin_options']);
// Where should I try to update the option? And how?
echo '</div>';
}
//from add_settings_section parameter 3, in plugin_admin_init
function plugin_section_text() {
echo '<p>Main description of this section here.</p>';
}
//Function from 3 paramter of add_settings_field in plugin_admin_init that outputs input HTML
function plugin_setting_string() {
$options = get_option('plugin_options');
echo 'value of options is: ' . $options;
echo "<input id='plugin_text_string' name='plugin_options' size='40' type='text'
value='{$options}' />";
}
//Validation function from register_setting in plugin_admin_init
function plugin_options_validate($input) {
echo "this is input: " . $input;
$newinput = trim($input);
if(!preg_match('/^[0-9]+$/', $newinput)) {
$newinput = 10;
}
update_option('plugin_options', $newinput);
return $newinput;
}
Note: I don't actually use these exact function/variable names - I have them prefixed with the actual plugin name.
Can you help me? I want to take the information the admin inputs into the settings page and update the database with the that information (which in this case is just a single number). I understand that I need to use PHP _POST, but I don't know how. Furthermore, I don't where to post to, because when I use action="the_file_name.php," I get a 404 error upon submission. What should the action of the form be so that I can use the information that I got from the admin submission and use it for later? And after I do that, how do update the setting? And by how, I mean where do I place update_option? I apologize if this seems rambly or vague - I feel like I'm somewhat over my head.
If it helps, I've been building this settings page in the same file as the plugin itself, with the help of this guide: http://ottopress.com/2009/wordpress-settings-api-tutorial/
Unfortunately, I don't see anything in that guide that speaks of updating information, just creating the page itself.