Created
June 9, 2015 14:20
-
-
Save afuggetta/a57f31b3da1534618a4c to your computer and use it in GitHub Desktop.
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
<?php | |
//Update profile for WP Users | |
add_action( 'profile_update', array( $this, 'bps_users_save_metadata' ), 10, 2 ); | |
//Adding fields to normal user profile | |
add_action( 'show_user_profile', array( $this, 'bps_custom_user_profile_fields' ) ); | |
add_action( 'edit_user_profile', array( $this, 'bps_custom_user_profile_fields' ) ); | |
/** | |
* Saves additional user meta | |
* | |
* @param $user_id | |
* @param $old_user_data | |
*/ | |
public function bps_users_save_metadata( $user_id, $old_user_data ) { | |
if ( ! current_user_can( 'edit_posts' ) ) { | |
return; | |
} | |
if ( empty( $_POST['update_user_meta'] ) || ! wp_verify_nonce( $_POST['update_user_meta'], '_update_user_meta' ) ) { | |
return; | |
} | |
$fields = [ | |
// Blog Meta | |
'custom-img-id', | |
'user_job_title', | |
]; | |
foreach ( $fields as $field ) { | |
$field_name = '_' . str_replace( '-', '_', $field ); | |
if ( isset( $_POST[ $field ] ) ) { | |
update_user_meta( $user_id, $field_name, $_POST[ $field ] ); | |
} | |
if ( empty( $_POST[ $field ] ) ) { | |
delete_user_meta( $user_id, $field_name ); | |
} | |
} | |
} | |
/** | |
* Show custom user profile fields | |
* | |
* @param WP_User $user The user object. | |
* | |
* @return void | |
*/ | |
public function bps_custom_user_profile_fields( $user ) { | |
// See if there's a media id already saved as post meta | |
$your_img_id = get_user_meta( $user->ID, '_custom_img_id', true ); | |
$job_title = get_user_meta( $user->ID, '_user_job_title', true ); | |
?> | |
<table class="form-table"> | |
<tr> | |
<th> | |
Big header image | |
</th> | |
<td> | |
<div id="custom-img-container"> | |
<?php | |
if ( ! empty( $your_img_id ) ) : | |
echo wp_get_attachment_image( $your_img_id, 'small' ); | |
endif; | |
?> | |
</div> | |
<!-- Add & remove image links --> | |
<p class="hide-if-no-js"> | |
<a id="user-header-image" href="javascript:void(0);" class="button add-custom-img">Add media</a> | |
<a id="user-delete-custom-img" href="javascript:void(0);" | |
class="button delete-custom-img hidden"> | |
Remove this image | |
</a> | |
</p> | |
<br><span class="description">Set the big header image for the user profile</span> | |
<!-- A hidden input to set and post the chosen image id --> | |
<input class="custom-img-id" id="custom-img-id" name="custom-img-id" type="hidden" | |
value="<?php echo esc_html( $your_img_id ); ?>"/> | |
</td> | |
</tr> | |
<tr> | |
<th> | |
<label for="user_job_title">Job title</label> | |
</th> | |
<td> | |
<input type="text" name="user_job_title" id="user_job_title" | |
value="<?php echo esc_attr( $job_title ); ?>" /> | |
<br><span class="description">Set the user's job title</span> | |
</td> | |
</tr> | |
</table> | |
<?php | |
wp_nonce_field( '_update_user_meta', 'update_user_meta' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment