Skip to content

Instantly share code, notes, and snippets.

@wanye71
Created November 19, 2012 01:42
Show Gist options
  • Save wanye71/4108525 to your computer and use it in GitHub Desktop.
Save wanye71/4108525 to your computer and use it in GitHub Desktop.
Adds new fields for users
<?php
//hides the personal options
function hide_personal_options(){
echo "\n" . '<script type="text/javascript">jQuery(document).ready(function($) {
$(\'form#your-profile > h3:first\').hide();
$(\'form#your-profile > table:first\').hide();
$(\'form#your-profile\').show();
$(\'label[for=url], input#url\').hide();
});
</script>' . "\n";
}
add_action('admin_head','hide_personal_options');
function hide_profile_fields( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}
add_filter('user_contactmethods','hide_profile_fields',10,1);
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<!---- This is the Image for profile --->
<tr>
<th><label for="user_image">Profile Image</label></th>
<td>
<img src="<?php echo esc_attr( get_the_author_meta( 'user_image', $user->ID ) ); ?>" style="height:50px;">
<input type="text" name="user_image" id="user_image" value="<?php echo esc_attr( get_the_author_meta( 'user_image', $user->ID ) ); ?>" class="regular-text" /><input type='button' class="button-primary" value="Upload Image" id="uploadimage"/><br />
<span class="description">Please upload your image for your profile.</span>
</td>
</tr>
<!---------This is the Tagline for profiles----->
<tr>
<th><label for="image">Tagline</label></th>
<td>
<input type="text" name="user_tagline" id="user_tagline" value="<?php echo esc_attr( get_the_author_meta( 'user_tagline', $user->ID ) ); ?>" class="regular-text" />
<span class="description">Enter your tagline here.</span>
</td>
</tr>
<!---- This is the Gender for profile --->
<tr>
<th><label>Gender</label></th>
<td>
<input type="radio" name="user_gender" id="user_gender" value="Male" <?php if (esc_attr( get_the_author_meta( "user_gender", $user->ID )) == "Male") echo "checked"; ?> />
<label for="show_email">Male</label>
<input type="radio" name="user_gender" id="user_gender" value="Female" <?php if (esc_attr( get_the_author_meta( "user_gender", $user->ID )) == "Female") echo "checked"; ?> />
<label for="user_gender">Female</label>
</td>
</tr>
<!-------This is the Age Field for profiles-->
<tr>
<th><label for="image">Your Age</label></th>
<td>
<input type="text" name="user_age" id="user_age" value="<?php echo esc_attr( get_the_author_meta( 'user_age', $user->ID ) ); ?>" class="regular-text" />
<span class="description">Please type your age.</span>
</td>
</tr>
<!---------This is the Tagline for profiles----->
<tr>
<th><label for="dropdown">dropdown Select field</label></th>
<td>
<select name="user_workdays" id="user_workdays">
<option value="Monday" <?php echo ($selected == "Monday")? 'selected="selected"' : '' ?>>Monday</option>
<option value="Tuesday" <?php echo ($selected == "Tuesday")? 'selected="selected"' : '' ?>>Tuesday</option>
<option value="Wednesday" <?php echo ($selected == "Wednesday")? 'selected="selected"' : '' ?>>Wednesday</option>
<option value="Thursday" <?php echo ($selected == "Thursday")? 'selected="selected"' : '' ?>>Thursday</option>
<option value="Friday" <?php echo ($selected == "Friday")? 'selected="selected"' : '' ?>>Friday</option>
<option value="Saturday" <?php echo ($selected == "Saturday")? 'selected="selected"' : '' ?>>Saturday</option>
<span class="description">Simple text field</span>
</td>
</tr>
</table>
<?php }
function zkr_profile_upload_js() {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(document).find("input[id^='uploadimage']").live('click', function(){
//var num = this.id.split('-')[1];
formfield = jQuery('#user_image').attr('name');
tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#user_image').val(imgurl);
tb_remove();
}
return false;
});
});
</script>
<?php
}
add_action('admin_head','zkr_profile_upload_js');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_style('thickbox'); //thickbox styles css
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;
update_usermeta( $user_id, 'user_image', $_POST['user_image'] );
update_usermeta( $user_id, 'user_tagline', $_POST['user_tagline'] );
update_usermeta( $user_id, 'user_gender', $_POST['user_gender'] );
update_usermeta( $user_id, 'user_age', $_POST['user_age'] );
update_usermeta( $user_id, 'user_workdays', $_POST['user_workdays'] );
update_usermeta( $user_id, 'user_interest', $_POST['user_interest'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment