Last active
August 29, 2015 14:03
-
-
Save joemcgill/ec7f018875ceb0cbff3e to your computer and use it in GitHub Desktop.
WP Custom User Meta
This file contains hidden or 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
<?php | |
/** | |
* Add custom user fields/meta info | |
*/ | |
add_action( 'show_user_profile', 'wu_custom_user_meta', 9 ); | |
add_action( 'edit_user_profile', 'wu_custom_user_meta', 9 ); | |
function wu_custom_user_meta( $user ) { | |
// create nonce field | |
wp_nonce_field( 'wu_custom_user_meta', 'wu_meta_nonce' ); | |
?> | |
<h3>Custom Section Title</h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="field_name">Field Label</label></th> | |
<td> | |
<input type="text" name="field_name" id="field_name" value="<?php echo esc_attr( get_the_author_meta( 'field_name', $user->ID ) ); ?>" class="regular-text" /> | |
</td> | |
</tr> | |
</table> | |
<?php } | |
/** | |
* Save custom user fields/meta info | |
*/ | |
add_action( 'personal_options_update', 'wu_save_custom_user_meta' ); | |
add_action( 'edit_user_profile_update', 'wu_save_custom_user_meta' ); | |
function wu_save_custom_user_meta( $user_id ) { | |
// validate permissions | |
if ( !current_user_can( 'edit_user', $user_id ) ) { | |
return false; | |
} | |
// verify nonce | |
check_admin_referer( 'wu_custom_user_meta', 'wu_meta_nonce' ); | |
// update fields | |
update_usermeta( $user_id, 'field_name', $_POST['field_name'] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Props to this tutorial: http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields