A snippet to add user profile image

Home » Snippets » A snippet to add user profile image
0

Created with:

Visibility: 

public

Creator: Ahrale

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// Add a new field to the user profile page for uploading an avatar image
function atar4u_add_avatar_upload_field( $user ) {
    ?>
    <h3><?php esc_html_e( 'Avatar Image', 'atar4u' ); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="avatar_image"><?php esc_html_e( 'Upload Image', 'atar4u' ); ?></label></th>
            <td>
                <input type="file" id="avatar_image" name="avatar_image" />
                <p class="description"><?php esc_html_e( 'Upload a custom avatar image.', 'atar4u' ); ?></p>
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'atar4u_add_avatar_upload_field' );
add_action( 'edit_user_profile', 'atar4u_add_avatar_upload_field' );

// Save the uploaded avatar image
function wpturbo_save_avatar_image( $user_id ) {
    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return;
    }

    if ( ! empty( $_FILES['avatar_image']['name'] ) ) {
        $uploaded_file = $_FILES['avatar_image'];
        $upload_overrides = array( 'test_form' => false );
        $uploaded_file = wp_handle_upload( $uploaded_file, $upload_overrides );

        if ( isset( $uploaded_file['file'] ) ) {
            $attachment = array(
                'post_mime_type' => $uploaded_file['type'],
                'post_title'     => sanitize_file_name( $uploaded_file['file'] ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            );

            $attach_id = wp_insert_attachment( $attachment, $uploaded_file['file'], $user_id );

            require_once( ABSPATH . 'wp-admin/includes/image.php' );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file['file'] );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            // Set the uploaded image as the user's avatar
            update_user_meta( $user_id, 'wp_user_avatar', $attach_id );
        }
    }
}
add_action( 'personal_options_update', 'atar4u_save_avatar_image' );
add_action( 'edit_user_profile_update', 'atar4u_save_avatar_image' );

// Display the uploaded avatar image
function wpturbo_display_avatar_image( $avatar, $id_or_email, $size, $default, $alt ) {
    if ( is_numeric( $id_or_email ) ) {
        $user_id = $id_or_email;
    } elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
        $user_id = $id_or_email->user_id;
    } else {
        $user = get_user_by( 'email', $id_or_email );
        if ( ! $user ) {
            return $avatar;
        }
        $user_id = $user->ID;
    }

    $avatar_image_id = get_user_meta( $user_id, 'wp_user_avatar', true );
    if ( $avatar_image_id ) {
        $avatar = wp_get_attachment_image( $avatar_image_id, $size, false, array( 'alt' => $alt ) );
    }

    return $avatar;
}
add_filter( 'get_avatar', 'atar4u_display_avatar_image', 10, 5 );
				

You can copy and paste this code into your functions.php file or create a new file in your custom snippet plugin and paste the code there. Make sure to replace 'wpturbo' with your desired text domain.

This code will add a new field to the user profile page for uploading an avatar image. The uploaded image will be saved as a WordPress attachment and associated with the user using user meta. The uploaded avatar image will then be displayed using the get_avatar filter.

Register an account to save your snippets or go Pro to get more features.