Add multiple custom fields to the general settings page

What I would like to do is to add a few custom fields to the general settings.
This is the code that I’m using. It works alright but I just cant figure how to add more fields.

I would like to create two fields for now, one for the telephone number and the second one for address:

function register_fields()
{
    register_setting('general', 'my_first_field', 'esc_attr');
    add_settings_field('my_first_field', '<label for="my_first_field">'.__('My Field' , 'my_first_field' ).'</label>' , 'print_custom_field', 'general');
}

function print_custom_field()
{
    $value = get_option( 'my_first_field', '' );
    echo '<input type="text" id="my_first_field" name="my_first_field" value="' . $value . '" />';
}

add_filter('admin_init', 'register_fields');

The only way I managed to get it to work for multiple fields was to duplicate everything.

So then it would look like this:

function register_fields()
{
    register_setting('general', 'my_first_field', 'esc_attr');
    add_settings_field('my_first_field', '<label for="my_first_field">'.__('My Field' , 'my_first_field' ).'</label>' , 'print_first_field', 'general');

    register_setting('general', 'my_second_field', 'esc_attr');
    add_settings_field('my_second_field', '<label for="my_second_field">'.__('My Field' , 'my_second_field' ).'</label>' , 'print_second_field', 'general');
}

function print_first_field()
{
    $value = get_option( 'my_first_field', '' );
    echo '<input type="text" id="my_first_field" name="my_first_field" value="' . $value . '" />';
}

function print_second_field()
{
    $value = get_option( 'my_second_field', '' );
    echo '<input type="text" id="my_second_field" name="my_second_field" value="' . $value . '" />';
}

add_filter('admin_init', 'register_fields');

But this is probably not the best way to do it, I tried creating a settings_section but It just didn’t work or didn’t save etc. Its just very confusing.

2

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