Users & Login PHP Intermediate

Add a Custom Field to User Profiles

Add an extra field, like a job title or phone number, to the user profile screen and save it with the user.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 26 lines
function nahnu_user_job_title_field( WP_User $user ): void {
	?>
	<h2>Extra details</h2>
	<table class="form-table" role="presentation">
		<tr>
			<th><label for="nahnu_job_title">Job title</label></th>
			<td>
				<input type="text" name="nahnu_job_title" id="nahnu_job_title" class="regular-text"
					value="<?php echo esc_attr( get_user_meta( $user->ID, 'nahnu_job_title', true ) ); ?>">
			</td>
		</tr>
	</table>
	<?php
}
add_action( 'show_user_profile', 'nahnu_user_job_title_field' );
add_action( 'edit_user_profile', 'nahnu_user_job_title_field' );

function nahnu_save_user_job_title( int $user_id ): void {
	if ( ! current_user_can( 'edit_user', $user_id ) || ! isset( $_POST['nahnu_job_title'] ) ) {
		return;
	}
	// WordPress checks the profile form's nonce before these actions run.
	update_user_meta( $user_id, 'nahnu_job_title', sanitize_text_field( wp_unslash( $_POST['nahnu_job_title'] ) ) );
}
add_action( 'personal_options_update', 'nahnu_save_user_job_title' );
add_action( 'edit_user_profile_update', 'nahnu_save_user_job_title' );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress profiles have a few built-in fields, like name, website and bio. If you need more, for a team page or a member directory, you can add your own without a plugin.

How it works

show_user_profile adds the field to your own profile, and edit_user_profile adds it when an admin edits someone else. The field’s value is loaded with get_user_meta().

When the form is saved, personal_options_update or edit_user_profile_update runs. The save function checks the current user can edit that profile, cleans the text with sanitize_text_field() and stores it as user meta.

Things to watch

  • Show the value on the front end with get_user_meta( $user_id, 'nahnu_job_title', true ), and escape it with esc_html().
  • For a phone number or URL, use type="tel" or type="url" and esc_url_raw() for URLs.

How to test it

Open Users → Profile. Fill in “Job title”, save, and check the value is still there after reloading.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only