Created
April 6, 2015 14:59
-
-
Save emanualjade/032681cfb5df09705d44 to your computer and use it in GitHub Desktop.
Add custom user fields to wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function custom_user_profile_fields($user){ | |
if(is_object($user)) | |
$company = esc_attr( get_the_author_meta( 'company', $user->ID ) ); | |
else | |
$company = null; | |
?> | |
<h3>Extra profile information</h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="company">Company Name</label></th> | |
<td> | |
<input type="text" class="regular-text" name="company" value="<?php echo $company; ?>" id="company" /><br /> | |
<span class="description">Where are you?</span> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'show_user_profile', 'custom_user_profile_fields' ); | |
add_action( 'edit_user_profile', 'custom_user_profile_fields' ); | |
add_action( "user_new_form", "custom_user_profile_fields" ); | |
function save_custom_user_profile_fields($user_id){ | |
# again do this only if you can | |
if(!current_user_can('manage_options')) | |
return false; | |
# save my custom field | |
update_user_meta($user_id, 'company', $_POST['company']); | |
} | |
add_action('user_register', 'save_custom_user_profile_fields'); | |
add_action('profile_update', 'save_custom_user_profile_fields'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment