Add image upload to default WordPress Registration form
Add image upload to default WordPress Registration form
I am trying to hook an image upload function to the default WordPress Registration form. This would then store in user meta to display in the user profile.
It seems my code should work, but whenever I upload during registration, the user is signed up by no image has been stored.
How can i fix my function to store the image on registration in user profile?
// Add IMage upload field to reg form
function add_image_upload_to_registration_form() {
?>
<p>
<label for="user_profile_picture">Profile Pic</label><br>
<input type="file" name="user_profile_picture" id="user_profile_picture" accept="image/*" />
</p>
<?php
}
add_action('register_form', 'add_image_upload_to_registration_form');
// Handle image upload & save to user meta
function save_profile_picture_on_registration($user_id) {
if (isset($_FILES['user_profile_picture']['tmp_name'])) {
$file = $_FILES['user_profile_picture']['tmp_name'];
if ($file) {
$attachment_id = media_handle_upload('user_profile_picture', $user_id);
if (!is_wp_error($attachment_id)) {
update_user_meta($user_id, 'profile_picture_attachment_id', $attachment_id);
}
}
}
}
add_action('user_register', 'save_profile_picture_on_registration');
// Display profile pic in user profile
function display_profile_picture_in_profile($user) {
?>
<h3>Profile Picture</h3>
<table class="form-table">
<tr>
<th><label for="user_profile_picture">Profile Picture</label></th>
<td>
<?php
$attachment_id = get_user_meta($user->ID, 'profile_picture_attachment_id', true);
if ($attachment_id) {
echo wp_get_attachment_image($attachment_id, 'thumbnail');
} else {
echo 'No profile picture uploaded.';
}
?>
</td>
</tr>
</table>
<?php
}
add_action('show_user_profile', 'display_profile_picture_in_profile');
add_action('edit_user_profile', 'display_profile_picture_in_profile');
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand