Skip to content

Instantly share code, notes, and snippets.

@orfeomorello
Created February 18, 2017 14:02
Show Gist options
  • Save orfeomorello/d5fb11dd1ebd263cdac0a9a8e7c5f010 to your computer and use it in GitHub Desktop.
Save orfeomorello/d5fb11dd1ebd263cdac0a9a8e7c5f010 to your computer and use it in GitHub Desktop.
/* How to add additional field in user profile add-edit in wordpress start */
add_action( 'show_user_profile', 'custom_user_fields' );
add_action( 'edit_user_profile', 'custom_user_fields' );
add_action( "user_new_form", "custom_user_fields" );
function custom_user_fields( $user )
{
$user_fb_link = get_the_author_meta( 'user_fb_link', $user->ID);
$user_gender = get_the_author_meta( 'user_gender', $user->ID);
?>
<h3><?php _e("Custom User information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="user_gender"><?php _e("Gender"); ?></label>
</th>
<td>
<select name="user_gender" id="user_gender" style="width:180px">
<option value="">-Select Gender-</option>
<option value="male" <?php selected( $user_gender, "male"); ?>>Male</option>
<option value="female" <?php selected( $user_gender, "female"); ?>>Female</option>
</select>
</td>
</tr>
<tr>
<th>
<label for="user_fb_link"><?php _e("Enter Your Fb Link"); ?></label>
</th>
<td>
<input type="text" name="user_fb_link" id="user_fb_link" size="60" value="<?php echo $user_fb_link;?>" />
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_custom_user_fields' );
add_action( 'edit_user_profile_update', 'save_custom_user_fields' );
add_action( 'user_register', 'save_custom_user_fields' );
function save_custom_user_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'user_gender', $_POST['user_gender'] );
update_user_meta( $user_id, 'user_fb_link', $_POST['user_fb_link'] );
}
/* How to add additional field in user profile add-edit in wordpress End */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment