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.
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.
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 toregister_setting
or it will not work.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.