Support

Account

Home Forums Front-end Issues Upload User Avatar Using update_field

Unread

Upload User Avatar Using update_field

  • Hi,

    I LOVE ACF and thanks to Elliot for developing such an awesome plugin! I’ve been using it for various projects and have also purchased a few add-ons (which I really like) but recently I have hit an obstacle and can’t seem to find a way around, despite searching this community, the official WordPress.org community and StackExchange.

    I have created a front-end user settings page where I’m using custom fields created by ACF. I’m successfully able to update all other input fields (including select menus, checkboxes, etc.) but can’t figure out for the life of me how to upload/update the user avatar field from the frontend. From the backend, it’s working flawlessly but when I try to use update_field to update the avatar, it removes the avatar uploaded from the backend instead of uploading the new avatar.

    I read the media_handle_upload documentation in the WordPress codex but don’t know how to make it work with the user settings page. All examples are on how to upload the image to a post. Could you please guide me how to upload the image to the user_meta using update_field?

    While searching on the web, I found this snippet of code which doesn’t work either:

    // Upload user avatar (functions.php)
    function my_update_attachment($f,$pid,$t='',$c='') {  
      wp_update_attachment_metadata( $pid, $f );  
      if( !empty( $_FILES[$f]['name'] )) { // New upload  
        require_once( ABSPATH . 'wp-admin/includes/file.php' );  
        include( ABSPATH . 'wp-admin/includes/image.php' );  
        $override['test_form'] = false;  
        $file = wp_handle_upload( $_FILES[$f], $override );  
      
        if ( isset( $file['error'] )) {  
          return new WP_Error( 'upload_error', $file['error'] );  
        }  
      
        $file_type = wp_check_filetype($_FILES[$f]['name'], array(  
          'jpg|jpeg' => 'image/jpeg',  
          'gif' => 'image/gif',  
          'png' => 'image/png',  
        ));
    
        if ($file_type['type']) {  
          $name_parts = pathinfo( $file['file'] );  
          $name = $file['filename'];  
          $type = $file['type'];  
          $title = $t ? $t : $name;  
          $content = $c;  
      
          $attachment = array(  
            'post_title' => $title,  
            'post_type' => 'attachment',  
            'post_content' => $content,  
            'post_parent' => $pid,  
            'post_mime_type' => $type,  
            'guid' => $file['url'],  
          );  
      
          foreach( get_intermediate_image_sizes() as $s ) {  
            $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );  
            $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options  
            $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options  
            $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options  
          }  
      
          $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );  
      
          foreach( $sizes as $size => $size_data ) {  
            $resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );  
            if ( $resized )  
              $metadata['sizes'][$size] = $resized;  
          }  
      
          $attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);  
      
          if ( !is_wp_error( $attach_id )) {  
            $attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );  
            wp_update_attachment_metadata( $attach_id, $attach_meta );  
          }  
         
    	   return array(  
    		  'pid' =>$pid,  
    		  'url' =>$file['url'],  
    		  'file'=>$file,  
    		  'attach_id'=>$attach_id  
    	   );
    	   
        }  
      }  
    }

    and then I’m using this code in the form processing part to update the avatar:

    if ( isset ( $_SESSION['avatar'] ) ) {
        	$att = my_update_attachment('avatar', 'user_'.$current_user->ID); 
    			update_field('field_5320739f7163d', $att['attach_id'], 'user_'.$current_user->ID);
        }

    But it throws this error:

    Fatal error: Cannot use object of type WP_Error as array in /Users/developer/www/local.com/wp-content/themes/v6/page-settings.php on line 41

    Please help! I’m stuck with this problem for a very long time (several weeks) and can’t find a solution for this.

    Thanks a lot!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.