Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

joemcgill commented Jul 14, 2014

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