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.
Exactly what I was searching for. Nice way to solve this by using a class.
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.
@michael
The functions.php file is where I’m going to put this. It would also work in a plugin.
Great snippet, thanks! How would I go about editing this to allow for multiple custom settings?
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.
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
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…
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.
thanks buddy….this is what i was looking for
Thank you! This was confusing the crap out of me trying to nut this one out. I could see the hook, but couldn’t latch onto the damn thing.
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.
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 !
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"]
@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
Pingback: Cómo añadir campos a las secciones de Ajustes (Settings) del administrador de WordPress | voragine.net
Why did you pass by reference? &$this