Adding custom field in General setting TAB in wordpress Adding custom field in General setting TAB in wordpress wordpress wordpress

Adding custom field in General setting TAB in wordpress


Stop the machines!

You're _doing_it_wrong(), although this function does not detect what you're doing :)
We don't touch core file, we do plugins. Please, restore your WP to a fresh state.


Answering to the Question title

Adding custom field in General setting TAB in wordpress

Follows an example from WordPress Answers. The key function is add_settings_field, right now it adds a full WP Editor, but it can be adjusted to show any kind of field. A nice article that explains the Settings API.

<?php/** * Plugin Name: My Custom General Setting */add_action( 'admin_init', 'wpse_57647_register_settings' );/*  * Register settings  */function wpse_57647_register_settings() {    register_setting(         'general',         'html_guidelines_message',        'esc_html' // <--- Customize this if there are multiple fields    );    add_settings_section(         'site-guide',         'Publishing Guidelines',         '__return_false',         'general'     );    add_settings_field(         'html_guidelines_message',         'Enter custom message',         'wpse_57647_print_text_editor',         'general',         'site-guide'     );}    /*  * Print settings field content  */function wpse_57647_print_text_editor() {    $the_guides = html_entity_decode( get_option( 'html_guidelines_message' ) );    echo wp_editor(         $the_guides,         'sitepublishingguidelines',         array( 'textarea_name' => 'html_guidelines_message' )     );}

resulting settings


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.