Create a new settings section on General Settings in WordPress

0
281

I had to add a new settings section on General Settings in WordPress. This is a easy task to be done but you have to write a bit of code. You do not need to change any wp-admin files because this will be rewritten when WordPress will update.

We will use Hooks and actions so we can add this option very easy

/* Admin init */
add_action( 'admin_init', 'my_new_general_settings_init' ); //getting the function to initialize

/* Settings Init */
function my_new_general_settings_init(){

    /* Register Settings */
    register_setting(
        'general', // Options group
        'my-option-name', // This is the name of the option. You can change it
        'my_option_sanitizegeneral' // sanitize callback function
    );

    /* Create settings section */
    add_settings_section(
        'my-section-id', // Section ID
        'My Additional Reading Settings', // Section title
        'my_settings_section_description', // Section callback function
        'general' // Settings page slug
    );

    /* Create settings field */
    add_settings_field(
        'my-settings-field-id', // Field ID
        'Gift section available?', // Field title
        'my_settings_field_callback', // Field callback function
        'general', // Settings page slug
        'my-section-id' // Section ID
    );
}

/* Sanitize Callback Function */
function my_option_sanitizegeneral( $input ){
    return isset( $input ) ? true : false;
}

/* Setting Section Description */
function my_settings_section_description(){
   //echo wpautop( "This aren't the Settings you're looking for. Move along." );
}

/* Settings Field Callback */
function my_settings_field_callback(){
    ?>
    <label for="droid-identification">
        <input id="droid-identification" type="checkbox" value="1" name="my-option-name" <?php checked( get_option( 'my-option-name', true ) ); ?>> Text for checkbox
    </label>
    <?php
}

Use this code and change it how you wish so it best fits your needs. Also you can change the input type as text or what ever you wish.

For the input to be change the function my_settings_field_callback to the one bellow:

/* Settings Field Callback */
function my_settings_field_callback(){
    ?>
    <label for="droid-identification">
        <input id="droid-identification" type="text" value="<?php echo get_option( 'my-option-name', true );?>" name="my-option-name">
    </label>
    <?php
}

For the field to be textarea you can use this:

/* Settings Field Callback */
function my_settings_field_callback(){
    ?>
    <label for="droid-identification">
        <textarea id="droid-identification"  name="my-option-name"><?php echo get_option( 'my-option-name', true );?></textarea>
    </label>
    <?php
}

Of course you can add multiple settings with multiple fields.

LEAVE A REPLY

Please enter your comment!
Please enter your name here