Need help understanding/coding with Settings API

So I did something like below, but I get just (I am expecting a Facebook textbox?)

Read More

I hope its not too messy. I am new to Settings API …

add_action('admin_init', function() {
    register_setting('elem_opts', 'elem_opts', 'elem_validate_opts');
  add_settings_section('elem_opts_form', 'Elements Theme Options', 'elem_opts_form_cb', 'elems_opts');
  add_settings_field('elem_opts_form', 'Facebook', 'elem_opts_social_fb_cb', 'elems_opts', 'elem_opts_form');
});

function elem_opts_social_fb_cb() {
  $opts = get_option('elem_opts');
  if (empty($opts) || !is_array($opts)) {
    $opts = array();
  }
  ?>
  <input type="text" name="elem_opts[fb]" value="<?php echo $opts['fb'] ? $opts['fb'] : ''; ?>" />
  <?php
}

add_action('admin_menu', function() {
    add_theme_page('Elements Options', 'Elements Options', 'manage_options', 'elem_opts', 'elem_opts_cb');
});

function elem_validate_opts($input) {
  return $input;
}

function elem_opts_form() {
  ?>
  <p>Enable social network links, by entering your username for the respective social networks</p>
  <?php
}

function elem_opts_cb() {
    ?>
    <div class="wrap">
        <h2>Elements Theme Options</h2>

        <form action="options.php" method="post">
            <?php 
            settings_fields('elem_opts'); 
            do_settings_sections('elem_opts');
            ?>
            <input type="submit" name="Submit" value="Save changes" />
        </form>
    </div>
    <?php
}

Related posts

Leave a Reply

2 comments

  1. You have some of the callback functions and the ID’s mixed up, try this:

    add_action('admin_init', function() {
        register_setting('elem_opts', 'elem_opts', 'elem_validate_opts');
        add_settings_section('elem_opts_form1', 'Elements Theme Options', 'elem_opts_form', 'elem_opts');
        add_settings_field('elem_opts_form', 'Facebook', 'elem_opts_social_fb_cb', 'elem_opts', 'elem_opts_form1');
    });
    
    function elem_opts_social_fb_cb() {
      $opts = get_option('elem_opts');
      if (empty($opts) || !is_array($opts)) {
        $opts = array();
      }
      ?>
      <input type="text" name="elem_opts[fb]" value="<?php echo $opts['fb'] ? $opts['fb'] : ''; ?>" />
      <?php
    }
    
    add_action('admin_menu', function() {
        add_theme_page('Elements Options', 'Elements Options', 'manage_options', 'elem_opts', 'elem_opts_cb');
    });
    
    function elem_validate_opts($input) {
      return $input;
    }
    
    function elem_opts_form() {
      ?>
      <p>Enable social network links, by entering your username for the respective social networks</p>
      <?php
    }
    
    function elem_opts_cb() {
        ?>
        <div class="wrap">
            <h2>Elements Theme Options</h2>
    
            <form action="options.php" method="post">
                <?php 
                settings_fields('elem_opts'); 
                do_settings_sections('elem_opts');
                ?>
                <input type="submit" name="Submit" value="Save changes" />
            </form>
        </div>
        <?php
    }
    

    which give me this:

    enter image description here

    and for next time try to use different names and ids for each function, callback, section and page slug.

  2. See here for reference: Incorporating the Settings API in WordPress Themes. In particular, see Page 10.

    Your first problem, though I don’t know that it would cause the form section/field not to output, is that you pass the same string as the first argument to both add_settings_section() and add_settings_field(). For add_settings_field(), the first argument is the ID of the setting field itself, so it probably shouldn’t be the same thing as the first argument passed to add_settings_section(), which is the ID of the settings section.