Add multiple custom fields to the general settings page

Well the second bit of code is technically the correct way to do it. However, at the end of the add_settings_field() you can pass arguments.

Please view the WordPress Add_Settings_Field function reference. This will help you in getting the best understanding of how the add_settings_field() function really works.

Now, with that said, you could use a ‘shared’ function for your callback. Such as I do in my options page when I develop themes.

Here is an example of how I do it.

// My Example Fields
add_settings_field(  
    'tutorial_display_count',                      
    'Tutorial Display Count',               
    'ch_essentials_textbox_callback',   
    'ch_essentials_front_page_option',                     
    'ch_essentials_front_page',
    array(
        'tutorial_display_count' // $args for callback
    ) 
);
add_settings_field(  
    'blog_display_count',                      
    'Blog Display Count',               
    'ch_essentials_textbox_callback',   
    'ch_essentials_front_page_option',                     
    'ch_essentials_front_page',
    array(
        'blog_display_count'  // $args for callback
    ) 
);

// My Shared Callback
function ch_essentials_textbox_callback($args) { 
 
$options = get_option('ch_essentials_front_page_option'); 

echo '<input type="text" id="'  . $args[0] . '" name="ch_essentials_front_page_option['  . $args[0] . ']" value="' . $options[''  . $args[0] . ''] . '">';
 
}

It will take a little bit of customizing to fit your needs, but doing a shared function for your callbacks will save a lot of space in terms of code. Other than that, you are doing it correctly as is.

–Edit–

Ok, this is what it should be like for you.. just modify the code as needed, I wrote this on the fly.. I did test it to check, and it worked. You just need to modify the add_settings_field(s) to suit your needs. If you need to add more, just copy and paste one and edit it. Make sure to register_setting or it will not work.

add_action('admin_init', 'my_general_section');  
function my_general_section() {  
    add_settings_section(  
        'my_settings_section', // Section ID 
        'My Options Title', // Section Title
        'my_section_options_callback', // Callback
        'general' // What Page?  This makes the section show up on the General Settings Page
    );
    
    add_settings_field( // Option 1
        'option_1', // Option ID
        'Option 1', // Label
        'my_textbox_callback', // !important - This is where the args go!
        'general', // Page it will be displayed (General Settings)
        'my_settings_section', // Name of our section
        array( // The $args
            'option_1' // Should match Option ID
        )  
    ); 
    
    add_settings_field( // Option 2
        'option_2', // Option ID
        'Option 2', // Label
        'my_textbox_callback', // !important - This is where the args go!
        'general', // Page it will be displayed
        'my_settings_section', // Name of our section (General Settings)
        array( // The $args
            'option_2' // Should match Option ID
        )  
    ); 
    
    register_setting('general','option_1', 'esc_attr');
    register_setting('general','option_2', 'esc_attr');
}

function my_section_options_callback() { // Section Callback
    echo '<p>A little message on editing info</p>';  
}

function my_textbox_callback($args) {  // Textbox Callback
    $option = get_option($args[0]);
    echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />';
}

Leave a Comment