Add Field to General Settings Page

28 responses

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.

28 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

  11. Brian

    To try to answer some of the questions…
    Angel, I don’t know if you figured it out, but maybe this will help someone else if you have.

    You’re over writing the $nap variable each time you call the new_general_setting() function, that’s why you only end up with the last one. The code isn’t run through all at once, so by the time the fields_html() function is run, there is only the last entry. I liked where you were going with your code, so I have it working like this:

    $address = new new_general_setting(‘address’);
    $city = new new_general_setting(‘city’);
    $state = new new_general_setting(‘state’);
    $phone = new 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 , $this->nap1 ).” , array(&$this, ‘fields_html’) , ‘general’ );
    }

    function fields_html() {
    $value = get_option( $this->nap1 , ” );
    echo ‘nap1 .’” name=”‘. $this->nap1 .’” value=”‘ . $value . ‘” />’;
    }
    }

    It’s really just creating an instance for each setting. Seems to be working for me.

    For anyone having trouble accessing this, I put this in my theme:

    Obviously change the ‘fav_color’ to whatever you called your option in the register_setting function.

    Hope that helps!

    Reply
  12. Pingback: Добавить поле на главную страницу админ-панели | Blog

  13. v4ssi

    Brian and Angel solution modified again for my needs. Ty to all.

    $address = new new_general_setting(‘address’);
    $phone = new new_general_setting(‘phone’);
    $fax = new new_general_setting(‘fax’);

    class new_general_setting {
    private $var = ”;
    function new_general_setting($var) {
    $this->var = $var;
    add_filter( ‘admin_init’ , array( &$this , ‘register_fields’ ) );
    }
    function register_fields() {
    $fields = array(‘address’ => ‘Address’, ‘phone’ => ‘Phone Number’, ‘fax’ => ‘Fax Number’);
    register_setting( ‘general’, $this->var, ‘esc_attr’ );
    add_settings_field($this->var, ”.__($fields[$this->var] , $this->var).” , array(&$this, ‘fields_html’) , ‘general’ );
    }
    function fields_html() {
    $value = get_option($this->var, ” );
    echo ‘var.’” name=”‘.$this->var.’” value=”‘ . $value . ‘” />’;
    }
    }

    Reply
  14. Eduardo Segura

    Well, this save me much time, so this is the modified version with multiples values :)


    class new_general_setting
    {
    private $f_id;
    private $f_name;
    private $f_description;

    function new_general_setting($array)
    {
    $this->f_id = $array[0];
    $this->f_name = $array[1];
    $this->f_description = $array[2];

    add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
    }

    function register_fields()
    {
    register_setting( 'general', $this->f_name, 'esc_attr' );
    add_settings_field( $this->f_id, 'f_name.'">'.__($this->f_description, $this->f_name ).'' , array(&$this, 'fields_html') , 'general' );
    }

    function fields_html()
    {
    $value = get_option( $this->f_name, '' );
    echo 'f_name.'" name="'.$this->f_name.'" value="' . $value . '" />';
    }
    }

    $facebook = new new_general_setting( array('facebook_url', 'facebook_icon', 'Endereço do facebook') );

    Reply
  15. geomagas

    Concise, straightforward and working!
    You might want to add a class="regular-text ltr" to the input field, for consistency, but that’s really minor.
    All and all, this is definitely a thumbs-up!
    Thanks!

    Reply
  16. Pingback: Add Custom Fields to General Settings Page in WordPress | GetProgram.in

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>

0
0

Created by Gabriele Cirulli. Based on 1024 by Veewo Studio and conceptually similar to Threes by Asher Vollmer.