I’ve created a simple settings page with just one page, one section and two fields, it all displays just fine, but I can’t for the life of me figure out why it won’t save. There’s either a typo or I’m missing a whole function or something similar.
My code:
test-page.php
<?php
//add menu page with no sub-pages
function add_test_pages() {
add_menu_page( 'Test Page', 'Test Page', 'edit_posts', 'test_page', $function = 'add_test_page', $icon_url="dashicons-edit", $position = null );
}
add_action( 'admin_menu', 'add_test_pages' );
//Set up Section and Field Settings
function test_page_settings () {
register_setting( 'test_page', 'field_1');
register_setting( 'test_page', 'field_2');
add_settings_section( 'test-section1', 'Section 1', 'section_1', 'test_page' );
add_settings_field( 'field-1', 'Field 1', 'field_1', 'test_page', $section = 'test-section1' );
add_settings_field( 'field-2', 'Field 2', 'field_2', 'test_page', $section = 'test-section1' );
}
add_action( 'admin_init', 'test_page_settings' );
//section 1
function section_1(){
echo 'section 1 test';
}
//field functions
function field_1 () {
$field1 = esc_attr(get_option('field_1'));
echo '<input type="text" name="field_1" value"'.$field1.'" placeholder="Field 1" />';
}
function field_2 () {
$field2 = esc_attr(get_option('field_2'));
echo '<input type="text" name="field_2" value"'.$field2.'" placeholder="Field 2" />';
}
//add template
function add_test_page() {
require_once( 'html-templates/test-page-template.php' );
}
and my template, test-page-template.php
<h1>My Test Template</h1>
<br/>
<hr />
<?php settings_errors(); ?>
<form action="options.php" method="post">
<?php settings_fields( 'test_page' ); ?>
<?php do_settings_sections( 'test_page' ); ?>
<?php submit_button(); ?>
</form>
Thank you so much in advance for any help.
1 Answer
For anyone else that may not have read the above comments and are having a possibly similar issue, @epilektric has answered the question stating that the =
sign was missing after the value
attribute under the field_1
and field_2
functions.