Need help understanding/coding with Settings API

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.

Leave a Comment