How to add field to user profile page

Justin Tadlock published a piece of code I needed recently. This snippet allows you to add any fields to the user’s profile page.

<?php 
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

	<h3>Extra profile information</h3>

	<table class="form-table">

		<tr>
			<th><label for="twitter">Classes</label></th>

			<td>
				<input type="text" name="classes" id="classes" value="<?php echo esc_attr( get_the_author_meta( 'classes', $user->ID ) ); ?>" class="regular-text" /><br />
				<span class="description">Please enter your Classes' URL</span>
			</td>
		</tr>

	</table>
<?php }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

	if ( !current_user_can( 'edit_user', $user_id ) )
		return false;

	/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
	update_usermeta( $user_id, 'classes', $_POST['classes'] );
}

?>

Just copy and paste the code in to the functions.php.

  


One thought on “How to add field to user profile page

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>