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


//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>
<hr />
<?php settings_errors(); ?>
<form  action="options.php" method="post">
  <?php settings_fields( 'test_page' ); ?>
  <?php do_settings_sections( 'test_page' ); ?>
  <?php submit_button(); ?>

Thank you so much in advance for any help.

  • 1
    The equal sign after "value" is missing in the field_1 and field_2 functions.
    – epilektric
    Mar 31 '21 at 21:45
  • Yes, that was all it was. Thank you, you've saved me a third evening of angrily staring at my code.
    – Lois Hunt
    Apr 1 '21 at 13:43
  • Glad it helped. It happens to all of us. I once spent 45 minutes troubleshooting some Javascript that worked in Firefox but not IE. It was a comma. One superfluous comma at the end of a very long list of comma separated items.
    – epilektric
    Apr 2 '21 at 0:58

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.