Skip to content

Instantly share code, notes, and snippets.

@joemcgill
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemcgill/ec7f018875ceb0cbff3e to your computer and use it in GitHub Desktop.
Save joemcgill/ec7f018875ceb0cbff3e to your computer and use it in GitHub Desktop.
WP Custom User Meta
<?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'] );
}
@joemcgill
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment