Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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
}
share|improve this question
1  
And where's the Q? –  kaiser May 28 '11 at 12:58
    
It appears that the problem is that his settings section/setting field aren't being output - and thus, the question is: why? –  Chip Bennett May 28 '11 at 13:22
    
@kaiser, From the code, I am expecting the form to have a facebook textbox. Its not there. Why? –  Jiew Meng May 28 '11 at 13:22
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks, this works. I had typos etc ... elem became elems, and like you said messed up IDs and callbacks. –  Jiew Meng May 29 '11 at 2:17
add comment

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.

share|improve this answer
    
Thanks for alerting me to that. pastie.org/1984876 better? its still not working tho ... –  Jiew Meng May 28 '11 at 13:28
    
Yeah, I wasn't sure that would cause the form section/fields not to output. I need to dig in a bit deeper. I will take a look, and update my answer with whatever else I find. –  Chip Bennett May 28 '11 at 13:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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