Reputation: 342
I'm new to creating wordpress plugin. I want to create a plugin that display custom page option under setting options of wp-admin section and to save the value of input data and retrieve it, I have gone through the tutorial of otto press
I created a folder in wp-content/plugin/
named new-setting-plugin
inside the folder is a file named new-setting-plugin.php
so the entire file path is wp-content/plugin/new-setting-plugin/new-setting-plugin.php
the code of the file new-setting-plugin.php is given at the end of the table. After creating the code I went onto plugin page of wp-admin and installed the plugin, it was all fine.
Now when i press "Save setting" button it shows the message "Setting saved" but i can't see the value inside the input fields
I'm attaching my images to preview what it is appearing in my plugin page
When go to the plugin page i see the following input fields:
Now I'm entering the value to input fields:
When I clicked the "Save Setting" button i got the following message after page refreshed
Here is my code
<?php
add_action('admin_menu', 'add_page');
if ( !function_exists( 'add_page' ) ) {
//function to add page under setting options in wordpress admin section
function add_page() {
add_options_page('New Setting Page', 'New Setting', 'manage_options', 'plugin', 'plugin_options_frontpage');
}
}
function plugin_options_frontpage() {
?>
<div class="wrap">
<?php screen_icon('users'); ?><h2>New Setting Page title</h2>
<form action="options.php" method="post">
<?php settings_fields('plugin_options'); ?>
<?php do_settings_sections('plugin'); ?>
<table class="form-table">
<tr valign="top">
<td colspan="2">
<input name="Submit" type="submit" class="button button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
</td>
</tr>
</table>
</form>
</div>
<?php
}
add_action('admin_init', 'plugin_admin_init');
function plugin_admin_init(){
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_input1', 'Input 1', 'plugin_input1', 'plugin', 'plugin_main');
add_settings_field('plugin_text_input2', 'Input 2', 'plugin_input2', 'plugin', 'plugin_main');
}
function plugin_section_text() {
echo '<p>New input setting to be saved.</p>';
}
function plugin_input1() {
$options = get_option('plugin_options');
echo "<input id='plugin_input1' class='normal-text code' name='plugin_options[text_string]' size='30' type='text' value='{$options['text_string']}' />";
}
function plugin_input2() {
$options = get_option('plugin_options');
echo "<input id='plugin_input2' class='normal-text code' name='plugin_options[text_string]' size='30' type='text' value='{$options['text_string']}' />";
}
function plugin_options_validate($input) {
$options = get_option('plugin_options');
$options['text_string'] = trim($input['text_string']);
if(!preg_match('/^[a-z0-9]{32}$/i', $options['text_string'])) {
$options['text_string'] = '';
}
return $options;
}
?>
What wrong in my code, How can I correct my code and is there a way to show the value of input fields outside the field in same page in a table?
Upvotes: 3
Views: 16016
Reputation: 11
You have to change the 'name' of the inputs :
function manage_lists_cc_input1() {
$options = get_option('manage_lists_cc_options');
echo "<input id='manage_lists_cc_input1' class='normal-text code' name='manage_lists_cc_options[0]' size='30' type='text' value='{$options['text_string_0']}' />";
}
function manage_lists_cc_input2() {
$options = get_option('manage_lists_cc_options');
echo "<input id='manage_lists_cc_input2' class='normal-text code' name='manage_lists_cc_options[1]' size='30' type='text' value='{$options['text_string_1']}' />";
And just change the validate script to get the values (this is a simple example without validation ) :
function manage_lists_cc_options_validate($input) {
$options['text_string_0'] = $input[0];
$options['text_string_1'] = $input[1];
return $options;
}
Upvotes: 0
Reputation: 1
check the validation criteria.
preg_match('/^[a-z0-9]{32}$/i', $options['text_string'])
Comment the code inside validation function and test.
function plugin_options_validate($input) {
$options = get_option('plugin_options');
//$options['text_string'] = trim($input['text_string']);
//if(!preg_match('/^[a-z0-9]{32}$/i', $options['text_string'])) {
//$options['text_string'] = '';
//}
return $options;
}
Upvotes: 0
Reputation: 34627
You won't see them there. You have to get_options
.
For your code, if you do var_export( get_option('plugin_options') );
you'll see those saved settings/values.
Upvotes: 4