• Resolved lwynne

    (@lwynne)


    I am using the code below to add an extra field in my settings. I am attempting to add a second field (while keeping the first) and I’m unsure what the proper way to do so is. Any help?

    add_filter('admin_init', 'my_general_settings_register_fields');
    
    function my_general_settings_register_fields()
    {
        register_setting('general', 'my_field', 'esc_attr');
        add_settings_field('my_field', '<label for="my_field">'.__('My Field' , 'my_field' ).'</label>' , 'my_general_settings_fields_html', 'general');
    
    }
    
    function my_general_settings_fields_html()
    {
        $value = get_option( 'my_field', '' );
        echo '<input type="text" id="my_field" name="my_field" value="' . $value . '" />';
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • I think you have two options:

    First, if you would like the data to be stored in separate records in the wp_options table, you will have to duplicate the above code with different function and field names.

    Otherwise, my preference, is to store your data in a single record, as an array. This could be done like so …

    add_filter('admin_init', 'my_general_settings_register_fields');
    
    function my_general_settings_register_fields()
    {
        register_setting('general', 'my_fields', 'esc_attr');
        add_settings_field('my_fields', '<label for="my_fields">'.__('My Fields' , 'my_fields' ).'</label>' , 'my_general_settings_fields_html', 'general');
    
    }
    
    function my_general_settings_fields_html()
    {
        $value = get_option( 'my_fields', '' );
        echo '<input type="text" id="field_1" name="my_fields[field_1]" value="' . $value . '" />';
        echo '<input type="text" id="field_2" name="my_fields[field_2]" value="' . $value . '" />';
    }
    Thread Starter lwynne

    (@lwynne)

    The code above produced one label which says “My Fields” with two text boxes directly after it.

    How would I change this code so it produces to boxes under two separate fields (one field per box)? I’m trying to make individual fields…. how it is stored table-wise doesn’t make a difference to me.

    How about something like this?

    add_filter('admin_init', 'my_general_settings_register_fields');
    
    function my_general_settings_register_fields()
    {
        register_setting('general', 'my_fields', 'esc_attr');
        add_settings_field('my_fields', __('My Fields' , 'my_fields' ) , 'my_general_settings_fields_html', 'general');
    
    }
    
    function my_general_settings_fields_html()
    {
        $value = get_option( 'my_fields', '' );
        echo '<label for="field_1">' . __('Field 1' , 'my_fields' ) . '<input type="text" id="field_1" name="my_fields[field_1]" value="' . $value . '" /></label>';
        echo '<br>';
        echo '<label for="field_1">' . __('Field 2' , 'my_fields' ) . '<input type="text" id="field_2" name="my_fields[field_2]" value="' . $value . '" /></label>';
    }
    Thread Starter lwynne

    (@lwynne)

    I’m probably not being clear enough here…

    How do I replicate a single field with the bolded “My Fields” label? I don’t want two fields filed under one category.

    Ah, that would be the first option I mentioned above. This will save each field to its own option in the database.

    function my_general_settings_register_fields()
    {
        // Register first field
        register_setting('general', 'my_field_1', 'esc_attr');
        add_settings_field('my_field_1', '<label for="my_field_1">'.__('My Field 1' , 'my_fields' ).'</label>' , 'my_general_settings_field_1_html', 'general');
    
        // Regiester second field
        register_setting('general', 'my_field_2', 'esc_attr');
        add_settings_field('my_field_2', '<label for="my_field_2">'.__('My Field 2' , 'my_fields' ).'</label>' , 'my_general_settings_field_2_html', 'general');
    
    }
    add_filter('admin_init', 'my_general_settings_register_fields');
    
    function my_general_settings_field_1_html()
    {
        $value = get_option( 'my_field_1', '' );
        echo '<input type="text" id="my_field_1" name="my_field_1" value="' . $value . '" />';
    }
    
    function my_general_settings_field_2_html()
    {
        $value = get_option( 'my_field_2', '' );
        echo '<input type="text" id="my_field_2" name="my_field_2" value="' . $value . '" />';
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding More than One Custom Settings Field’ is closed to new replies.