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.

35 thoughts on “Add Field to General Settings Page”

  1. 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.

  2. 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.

  3. 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

  4. 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…

  5. 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.

  6. 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.

  7. 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 !

  8. 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"]

  9. @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

  10. 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!

  11. 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 . ‘” />’;
    }
    }

  12. 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') );

  13. 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!

  14. How do I change the text box in the setting page into and image uploader.

    I just wanted an option to add image instead.

    Thanks a lot. You code works sweet!

  15. Here is my fully working implementation of this code added in functions.php:

    /**
    * Add fields to General Settings page
    *
    * @reference
    */

    $nf_phone = new General_Settings_Field(‘phone’, ‘Telefone’);
    $nf_email = new General_Settings_Field(’email’, ‘E-mail de contato’);
    $nf_street = new General_Settings_Field(‘street’, ‘Rua’);
    $nf_neighborhood = new General_Settings_Field(‘neighborhood’, ‘Bairro’);
    $nf_cep = new General_Settings_Field(‘zipcode’, ‘CEP’);
    $nf_city = new General_Settings_Field(‘city’, ‘Cidade’);
    $nf_state = new General_Settings_Field(‘state’, ‘Estado’);

    $nf_phone->init();
    $nf_email->init();
    $nf_street->init();
    $nf_neighborhood->init();
    $nf_cep->init();
    $nf_city->init();
    $nf_state->init();

    class General_Settings_Field {

    protected $name;
    protected $value;

    public function __construct($name, $value = null)
    {
    $this->setName($name);

    if ($value == null)
    $this->setValue( $name );
    else
    $this->setValue( $value );
    }

    public function setName($name) {
    $this->name = $name;
    }

    public function setValue($value) {
    $this->value = $value;
    }

    public function getName() {
    return $this->name;
    }

    public function getValue() {
    return $this->value;
    }

    public function init() {
    add_filter( ‘admin_init’ , array( &$this , ‘register_fields’ ) );
    }

    public function register_fields()
    {
    register_setting( ‘general’, $this->getName(), ‘esc_attr’ );
    add_settings_field( $this->getName(), ”.__( ucfirst($this->getValue() ) , $this->getName() ).” , array(&$this, ‘fields_html’) , ‘general’ );
    }

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

    }

    /**
    * 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($field)
    {
    return get_option($field[‘key’]);
    }
    add_shortcode( ‘bloginfo’, ‘handle_shortcode_bloginfo’ );

    1. Hi Cesar Kohl, I’m having some problems with the echo statement. Is the first quotation mark correct? Is it supposed to print out a text field with labels?

      1. Mark, it looks like the site stripped off the html from all of the responses…the echo should start with an input html element.

        If you look up at the main article, you can see the correct statement in the “function fields_html()” section.

Leave a Reply to Anushka Cancel reply

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

%d bloggers like this: