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.

I can't find a more or less clear example on how to create additional fields, so may be someone can help me here? Here is my code:

function __construct(){
    add_action('admin_init', array($this, 'plugin_admin_init'));
}

public function create_fields(){
    register_setting('dleToWp_options', 'database_name');
    register_setting('dleToWp_options', 'database_prefix');
    register_setting('dleToWp_options', 'version');
    //Stuck here
    // add_settings_section('dleToWp_options', __('Settings', 'dleToWp'), NULL, 'dleToWp');
    // add_settings_field('plugin_text_string', 'Plugin Text Input', 'plugin_setting_string', 'plugin', 'dleToWp');
}

public function settings_page(){

    echo '<div class="wrap">
        <div class="icon32" id="icon-tools"></div>
        <h2>'.__('Database converter', 'dleToWp').'</h2>
        <form action="options.php" method="post">';
    settings_fields('dleToWp_options');
    do_settings_sections('dleToWp');
    echo '</form></div>';
}

I nee create 3 fields: Database name, database prefix and select box of versions.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

There are a lot of things which you have to understand about Settings API. Instead of duplicating information, I would strongly recommend you to read through bunch of articles called The Complete Guide To The WordPress Settings API, especially pay attention to the second part called Sections, Fields, and Settings. Don't hesitate to implement examples in articles, it will help you to understand and remember it better.

share|improve this answer
    
thanks a lot man - yours links was very helpful! –  user1692333 Jan 1 '13 at 21:30
    
@user1692333 you are welcome! –  Eugene Manuilov Jan 1 '13 at 21:40

Pretty close - but in your settings_page function, you have to add the html form elements for each field after the settings_fields call. And remove the "do_settings_sections" line. And a submit button is always a nice addition as well.

Here is an example that adds one of your fields:

?>
<table width="100%">
    <tr>
        <td>
            <label for="database_name" />Database Name</label>
            <input type="text" id ="database_name" size="50" name="database_name" value="<? echo get_option('database_name')?>" /> 
        </td>
    </tr>
</table>
<p class="submit">
    <input type="submit" class="button-primary" value="Save Changes" />
</p>
<?php

--- UPDATED ---

Instead of creating the form as described, it is also possible to use the add_settings_sections and add_settings_fields calls instead. This is how that works:

public function settings_page(){
    echo '<div class="wrap">
    <div class="icon32" id="icon-tools"></div>
    <h2>'.__('Database converter', 'dleToWp').'</h2>
    <form action="options.php" method="post">';
    settings_fields('dleToWp_options');
    do_settings_sections('dleToWp');
    <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>"  /></p>
    echo '</form></div>';
}

This is pretty close to the code you provided at the start except that there is now a submit button added.

In your create_fields function, you will also need to define the settings and the fields:

add_settings_section('dleToWp_options', 'Plugin Options', 'dleToWp_options_callback', 'dleToWp');
add_settings_field('dltToWp_database_name', 'Database Name', 'dleToWp_database_name_callback', 'dleToWp', 'dleToWp_options');

Notice that each call specifies the settings section and the add_settings_field has to point back to the section.

Finally, you have to create the callbacks for the section and for the field:

function dleToWp_options_callback() {
    echo "<p>Description of this Section</p>"
}

function dleToWp_database_name_callback() {
    echo "<input type='text' id='database_name' size='50' name='database_name' value='".get_option('database_name')."' />";
}

You still have to supply the html for the form elements. And you have to add your submit button html as well.

All this code is off the top of my head, so no guarantee that it works. But it should give you an idea of how to use the add_settings* functions and how to structure the callbacks.

share|improve this answer
    
Yes, you can do this. It's not flexible. –  chrisguitarguy Jan 1 '13 at 20:33
    
but i want to know how to do this using add_settings_section and add_settings_field as it is new API settings. –  user1692333 Jan 1 '13 at 20:35
    
Well, it isn't really new - those were added in 2.7, but I'll update my answer and demonstrate how to use those. –  Ed Burns Jan 1 '13 at 20:49

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.