Last active
March 21, 2017 19:32
-
-
Save dankerizer/1dec4bb37326c39c39e3024bb0b5359d to your computer and use it in GitHub Desktop.
add custom WordPress user fields
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
<?php | |
/** | |
* ampinstant Project | |
* @package ampinstant | |
* User: dankerizer | |
* Date: 18/03/2017 / 03.36 | |
*/ | |
function wpinstant_extra_profile_fields( $user ) { ?> | |
<h3>Extra profile information</h3> | |
<table class="form-table"> | |
<?php | |
$fields = array('twitter'=> 'https://twitter.com/username','facebook' => 'https://www.facebook.com/username','youtube'=> 'https://www.youtube.com/username','google-plus' => 'https://plus.google.com/+hadiedanker','whatsapp' => '+628522954555','github'=> 'https://github.com/username'); | |
foreach ($fields as $item=>$url){ | |
$desc = 'Please enter your '.ucwords($item).' URL'; | |
if($item == 'whatsapp'){ | |
$desc = 'Please enter your '.ucwords($item).' Number. must use <a target="_blank" href="https://en.wikipedia.org/wiki/List_of_country_calling_codes">country code</a> e.g +62 for Indonesia'; | |
} | |
echo '<tr>'; | |
echo ' <th><label for="'.$item.'">'.ucwords($item).'</label></th>'; | |
echo '<td>'; | |
echo '<input type="text" name="'.$item.'" id="'.$item.'" value="'.esc_attr( get_the_author_meta( $item, $user->ID ) ).'" class="regular-text" placeholder="'.$url.'" /><br />'; | |
echo '<p class="description">'.$desc.'</p>'; | |
echo '</td>'; | |
echo '</tr>'; | |
} | |
?> | |
<tr><td>Generate by AMPinstant</td></tr> | |
</table> | |
<?php } | |
add_action( 'personal_options_update', 'my_save_extra_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' ); | |
function my_save_extra_profile_fields( $user_id ) { | |
if ( !current_user_can( 'edit_user', $user_id ) ) | |
return false; | |
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */ | |
$fields = array('twitter'=> 'https://twitter.com/username','facebook' => 'https://www.facebook.com/username','youtube'=> 'https://www.youtube.com/username','google-plus' => 'https://plus.google.com/+hadiedanker','whatsapp' => '+628522954555','github'=> 'https://github.com/username'); | |
foreach ($fields as $item=>$url){ | |
update_usermeta( $user_id, $item, $_POST[$item] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment