Created
June 15, 2016 20:06
-
-
Save sisodiakaran/8bd9769563835ccc96af4e9df643dfa5 to your computer and use it in GitHub Desktop.
Function to add custom fields in Wordpress user profile editor
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
/** | |
* Function to display custom field in Admin Profile editor | |
* Only Admin can view these options | |
*/ | |
function user_custom_fields($user) { | |
if (is_admin()) { // Check of admin | |
$is_verified = get_the_author_meta('is_verified', $user->ID); | |
$is_qualified = get_the_author_meta('is_qualified', $user->ID); | |
?> <table class="form-table"> | |
<tr> | |
<th><label for="is_verified">Verified?</label></th> | |
<td><input id="is_verified" type="checkbox" name="is_verified" <?php echo $is_verified ? 'checked="checked"' : ''; ?> /></td> | |
</tr> | |
<tr> | |
<th><label for="is_qualified">Qualified?</label></th> | |
<td><input id="is_qualified" type="checkbox" name="is_qualified" <?php echo $is_qualified ? 'checked="checked"' : ''; ?> /></td> | |
</tr> | |
</table> | |
<?php | |
} | |
} | |
/** | |
* Function to save options | |
*/ | |
function save_user_custom_fields($user_id) { | |
if (!current_user_can('edit_user', $user_id)) { | |
return false; | |
} | |
update_user_meta($user_id, 'is_verified', ($_POST['is_verified'] ? true : false)); | |
update_user_meta($user_id, 'is_qualified', ($_POST['is_qualified'] ? true : false)); | |
} | |
add_action('show_user_profile', 'user_custom_fields'); | |
add_action( 'edit_user_profile', 'user_custom_fields' ); | |
add_action( 'edit_user_profile_update', 'save_user_custom_fields' ); | |
add_action( 'personal_options_update', 'save_user_custom_fields' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment