Add Field to General Settings Page

If you need to add an option to a site, but it doesn’t really need to be on its own page. You can probably add it to one of the existing settings pages. Here’s how to add an option to the General Settings page.

In this case, I’m adding a ‘favorite color’ field, probably not the best example, so go ahead and change that. :)

$new_general_setting = new new_general_setting();

class new_general_setting {
    function new_general_setting( ) {
        add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
    }
    function register_fields() {
        register_setting( 'general', 'favorite_color', 'esc_attr' );
        add_settings_field('fav_color', '<label for="favorite_color">'.__('Favorite Color?' , 'favorite_color' ).'</label>' , array(&$this, 'fields_html') , 'general' );
    }
    function fields_html() {
        $value = get_option( 'favorite_color', '' );
        echo '<input type="text" id="favorite_color" name="favorite_color" value="' . $value . '" />';
    }
}

The third argument of the register_setting() function is the name of the function that should be used to validate the input data, so customize that as needed.

16 thoughts on “Add Field to General Settings Page

  1. Michael

    Hi, could you please explain WHERE this code actually goes? It looks like it’s what I need but I’m not very versed in PHP or WordPress so it seems a bit abstract to me.

    Does it go into functions.php?

    Many thanks,
    Michael.

    Reply
  2. Liz

    Hi,

    Thanks for this, just what I needed. Do you know if I could order it to show up just underneath the tagline rather than at the bottom?

    Thanks.

    Reply
  3. swarkos

    Thanks for the snippet.
    I manage to display my custom field in the admin page but I cannot find how to access the content of my field in my theme page.

    Can you help me?
    Thanks

    Reply
  4. Nathan

    I get an “Unexpected T_CLASS” error when using your code exactly. Making a few modifications, I fixed that error but the fields don’t actually save…

    Reply
  5. Libin V Babu

    Hey,

    Its awesome man. This is what I’m looking for. Working perfectly.
    Can we customize the position of this element in General Settings tab? Currently it is showing at the bottom of page.

    Anyway thank you so much.

    Reply
  6. arsh

    Thanks for this code, it is working for add one field in the general setting page,
    can you please tell me how to add multiple fields on settings page.?

    Thanks in advance.

    Reply
  7. Angel

    I want to add more than one field in settings. I tried like this:

    $new_general_setting = new new_general_setting(‘address’);
    $new_general_setting->new_general_setting(‘city’);
    $new_general_setting->new_general_setting(‘state’);
    $new_general_setting->new_general_setting(‘phone’);

    class new_general_setting {
    private $nap1 =”;
    function new_general_setting($nap) {
    $this->nap1 = $nap;
    add_filter( ‘admin_init’ , array( &$this , ‘register_fields’ ) );
    }
    function register_fields() {
    register_setting( ‘general’, $this->nap1, ‘esc_attr’ );
    add_settings_field($this->nap1, ‘nap1 .’”>’.__($this->nap1 , ‘favorite_color’ ).” , array(&$this, ‘fields_html’) , ‘general’ );
    }
    function fields_html() {
    $value = get_option( $this->nap1 , ” );
    echo ‘nap1 .’” name=”‘. $this->nap1 .’” value=”‘ . $value . ‘” />’;
    }
    }

    but it shows only my last one! Please help me! There is nowhere else in the web that I can find a solution for this! Thanks !

    Reply
  8. Mobile Website Designer

    Very useful article, but you don’t say how we can access the new field in our posts/pages. For anyone wanting to know how to do that, add this code to your functions.php…


    /*
    * Function so we can get info from our WP Settings page (bloginfo) via a shortcode - e.g., [bloginfo key="fav_color"]
    */
    function handle_shortcode_bloginfo($a) {
    return get_option($a['key']);
    }
    add_shortcode( 'bloginfo', 'handle_shortcode_bloginfo' );

    …then in your page/post, you can just insert…

    [bloginfo key="fav_color"]

    Reply
  9. X3msnake

    @Libin V Babu

    There is a core function draft that is aiming at making easy to remove core setting fields, probably by removing and rearming the fields you can promote the ones you have just added.

    global $wp_settings_fields Stores the array of settings fields and info about their pages/sections.

    if you can find the array for the options page then you can manipulate the array to push your fields up.

    http://core.trac.wordpress.org/ticket/15865

    Reply
  10. Pingback: Cómo añadir campos a las secciones de Ajustes (Settings) del administrador de WordPress | voragine.net

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>