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.

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

I would like to create 2 fields for now, one for the telephone number and the second one for adress.

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 didnt work or didnt save etc. Its just very confusing. Please help me with this, thanks in advance.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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 arguements.

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] . ''] . '"></input>';

}

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 . '" />';
}
share|improve this answer
    
So what I dont understand is the 4th and 5th parameter in the add_settings_field(). I know the first one is the ID, second is the name, third is the callback to display it, but what are the next ones for? You have ch_essentials_front_page_option for both of them, I have just 'general' in the same place, next one is empty and the last one is now the args array. So now in the callback you have get_option with that value, but I dont know what to put there in my case. –  Richard Mišenčík Jan 10 at 11:30
1  
Edit has been made, that should get you 100% going. Let me know if you have any problems or questions. I did heavily comment it. –  MrJustin Jan 11 at 0:31
    
@MrJusting Thanks a lot. I actually got it working by looking at your profile and checking a questioin that you answered, "Implementing-tabs-on-custom-menu-page". It was very well commented, so then I compared my code and finally understood how it works. For me the page and section parameters were confusing, I though it was like section of the page and not the section of settings. So now I combined both together and added another value to $args, so first is the field id and the second is description, and then added another line to the callback function to echo the description with args[1] :) –  Richard Mišenčík Jan 12 at 21:04
    
Great, I'm glad you have figured it out, if you have any questions let me know. –  MrJustin Jan 13 at 15:38
    
I think Ill try now to create a separate menu page and add the options there, because in general and at the bottom its kind of lost. I'll let you know how did that go –  Richard Mišenčík Jan 13 at 16:20

Better way is use a wordpress options plugins. One of the best is Advanced Custom Fields.

http://www.advancedcustomfields.com/

If you buy a option page addon, then you can create a unlimited option page with lot of features. Please what out a video.

http://www.advancedcustomfields.com/add-ons/options-page/

Very usefull plugin and addon.

share|improve this answer
    
I just want to add a few fields, so plugin just for this would be an overkill for me but thanks. –  Richard Mišenčík Jan 10 at 11:28
1  
Not to mention this doesn't solve what the OP wanted, which was to add fields to the General Settings. AFAIK, ACF doesn't allow you to append fields tot he General Settings. –  NW Tech Jan 14 at 18:30

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.