Skip to content

Instantly share code, notes, and snippets.

@jdcauley
Last active November 6, 2018 06:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdcauley/6608b0a45581e4b8eaab to your computer and use it in GitHub Desktop.
Save jdcauley/6608b0a45581e4b8eaab to your computer and use it in GitHub Desktop.
User Profile WP
jQuery(document).ready(function($){
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('#upload_image_button').click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr('id').replace('_button','');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$('#image').val(attachment.url);
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open(button);
return false;
});
$('.add_media').on('click', function(){
_custom_media = false;
});
});
function extra_user_profile_fields($user){
$user_meta = get_user_meta($user->ID);
?>
<h3>Extra Author information</h3>
<table class="form-table">
<tr>
<th><label for="team">Include In Team?</label></th>
<td>
<?php if($user_meta['teammember'][0] == true): ?>
<input type="checkbox" name="teammember" id="teammember" value="true" checked/><br />
<?php else: ?>
<input type="checkbox" name="teammember" id="teammember" value="true" /><br />
<?php endif; ?>
<span class="description">Shows on About Page</span>
</td>
</tr>
<tr>
<th><label for="twitter">Twitter</label></th>
<td>
@<input type="text" name="twitter" id="twitter" value="<?php echo $user_meta['twitter'][0]; ?>" class="regular-text" /><br />
<span class="description">Please enter your Twitter username.</span>
</td>
</tr>
<tr>
<th><label for="image">User Profile Image</label></th>
<tr>
<td>
<?php if($user_meta['image'][0]): ?>
<img id="current-user-photo" src="<?php echo $user_meta['image'][0]; ?>">
<span class="description">User Pofile Photo</span>
<?php endif; ?>
<input type="hidden" name="image" id="image" value="<?php echo $user_meta['image'][0]; ?>" class="regular-text" /><br />
</td>
<td>
<button id="upload_image_button">Upload or Pick a Photo</button>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields_save($user_id){
update_user_meta($user_id, 'twitter', $_POST['twitter'] );
update_user_meta($user_id, 'image', $_POST['image'] );
update_user_meta($user_id, 'teammember', $_POST['teammember'] );
}
add_action( 'personal_options_update', 'extra_user_profile_fields_save' );
add_action( 'edit_user_profile_update', 'extra_user_profile_fields_save' );
function media_uploading_scripts(){
wp_enqueue_media();
wp_enqueue_script('admin_js', get_template_directory_uri() . '/assets/js/admin.js', array(), null, true);
}
add_action('admin_enqueue_scripts','media_uploading_scripts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment