Add settings fields on options discussion admin page

You have to call add_settings_section() first, pass a unique ID and assign it to the page (poor name) discussion:

add_settings_section( 'ads_id', 'Extra Settings', 'ads_description', 'discussion' );

Then register a callback to save your field(s) …

// Register a callback
register_setting(
    'discussion',
    'ads',
    'trim'
);

… and then register one or more fields:

// Register the field for the "avatars" section.
add_settings_field(
    'ads',
    'Test field',
    'ads_show_settings',
    'discussion',
    'ads_id',
    array ( 'label_for' => 'ads_id' )
);

Here is a very simple example:

add_action( 'admin_init', 'ads_register_setting' );

/**
 * Tell WP we use a setting - and where.
 */
function ads_register_setting()
{
    add_settings_section(
        'ads_id',
        'Extra Settings',
        'ads_description',
        'discussion'
    );

    // Register a callback
    register_setting(
        'discussion',
        'ads',
        'trim'
    );
    // Register the field for the "avatars" section.
    add_settings_field(
        'ads',
        'Test field',
        'ads_show_settings',
        'discussion',
        'ads_id',
        array ( 'label_for' => 'ads_id' )
    );
}

/**
 * Print the text before our field.
 */
function ads_description()
{
    ?><p class="description">This is the description</p><?php
}

/**
 * Show our field.
 *
 * @param array $args
 */
function ads_show_settings( $args )
{
    $data = esc_attr( get_option( 'ads', '' ) );

    printf(
        '<input type="text" name="ads" value="%1$s" id="%2$s" />',
        $data,
        $args['label_for']
    );
}

Note the fifth parameter for add_settings_field().

Result

enter image description here

Leave a Comment