You’re going to definitely want to do this with a plugin. Modifying WordPress Core files is an extremely bad idea as others have pointed out because every update will wipe them out and future versions may change the location/function of what you’re editing. You can get away by adding it to the theme’s functions.php
file as well but you run into the same problem as with WP Core, updates will wipe them out. Writing a Plugin is your best option.
Say you wanted to add a field for a user to define their favorite color. You first would do the following:
function my_extra_author_fields( $user ) { ?>
<h3>My Extra Author Fields</h3>
<table class="form-table">
<tr>
<th><label for="favorite_color">Favorite Color</label></th>
<td>
<input type="text" name="favorite_color" id="favorite_color" class="regular-text" value="<?php esc_attr( get_the_author_meta( 'favorite_color', $user->ID ) ); ?>" />
<br />
<span class="description">Please enter your favorite color</span>
</td>
</tr>
</table>
<?php }
add_action( 'show_user_profile', 'my_extra_author_fields' );
add_action( 'edit_user_profile', 'my_extra_author_fields' );
This will create the new fields in the form for user profile but will not actually save anything yet. That part comes next:
function save_my_extra_author_fields( $user_id ) {
// Check to see if user can edit this profile
if ( ! current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, 'favorite_color', $_POST['favorite_color'] );
}
add_action( 'personal_options_update', 'save_my_extra_author_fields' );
add_action( 'edit_user_profile_update', 'save_my_extra_author_fields' );
You will need to do a new <tr>
element and update_user_meta()
call for each custom field you want to add.
You can then access the value of these fields with get_the_author_meta( 'favorite_color' )
if you want to use the return programmatically such as testing to see if the favorite color is set or use the_author_meta( 'favorite_color' )
to simply echo it.