use add_settings_field properly?

I’m using add_settings_field to add some more details to a settings menu, but not sure how I save the settings, or call them back in my theme.

Here is the code thus far

add_action('admin_init','vimeo_setup');

function vimeo_setup(){
add_settings_field('vimeo_id','Vimeo ID','display_vimeo','general');
}

function display_vimeo(){

 echo '<input  type="text" name="vimeo_id" id="vimeo_id" value="" size="30" style="width:85%" />';
 echo '<p><small> Enter your Vimeo ID here.</small></p>';
}

Related posts

Leave a Reply

2 comments

  1. It should be noted that in order to add a settings field you must register that field to a registered section..

    So this must be used alongside your own registered section, or alternatively be a field to add to an existing section (such as those throughout the options pages).

    See.
    http://codex.wordpress.org/Function_Reference/add_settings_section

    I would go so far as to say these are not really very good when it comes to building plugin options pages or alike(theme options), simply because every registered section and field must be provided with a callback (imagine how many callbacks you’ll need if you have 3-4 sections all with 5-6 fields). You can of course make dynamic callbacks (so you only need one callback for a section and one for the fields) by passing along parameters… (you’ll also need to make sure you print the nonces for these sections/fields)..

    Personally i find it’s better to just register your options/settings, and print out your own sections/fields, and call a single nonce to cover all your settings..

    // 1: Reference, 2: Option name, 3: Callback function to sanitize the data
    register_setting( 'reference_name', 'option_name', 'example_callback' );
    

    Then wherever you print your various option fields, you only need print one nonce that covers the lot..

    settings_fields( 'reference_name' ); 
    

    If you registered various sections and fields you’d also have to do this for each of the registered sections.

    do_settings_sections('your_section');
    

    Really the only important aspect of setting up custom options is to make sure you define your own callback when registering the settings and appropriately sanitizing that data (third parameter for register_setting).