• pixel016

    (@pixel016)


    Hi everybody,

    I have a custom field “upload image” on the user profile page.
    When the user upload an image, it appear on the wordpress media library and on a featured image on a custom post.

    So the first upload is ok, the image update is ok but when we do the update when there is no new image it removes the old image. (sorry for my english).

    I use this code

    $thumbnail_id = get_post_thumbnail_id( $post_id );
    
                if ($thumbnail_id == "") {
                      $attach_id = wp_insert_attachment( $attachment, WP_CONTENT_DIR . '/uploads' . $filename, $post_id);
                      require_once( ABSPATH . 'wp-admin/includes/image.php' );
                      $attach_data = wp_generate_attachment_metadata( $attach_id, WP_CONTENT_DIR . '/uploads' . $filename );
                      wp_update_attachment_metadata( $attach_id, $attach_data );
                      set_post_thumbnail( $post_id, $attach_id );
                }
                else {
                      $attach_id = wp_insert_attachment( $attachment, WP_CONTENT_DIR . '/uploads' . $filename, $post_id);
                      require_once( ABSPATH . 'wp-admin/includes/image.php' );
                      $attach_data = wp_generate_attachment_metadata( $attach_id, WP_CONTENT_DIR . '/uploads' . $filename );
                        wp_delete_attachment($thumbnail_id );
                        wp_update_attachment_metadata( $attach_id, $attach_data );
                        set_post_thumbnail( $post_id, $attach_id );
    
                }
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You are currently deleting the attachment anytime it exists and the profile is updated. You should be checking if the current image in the attachment matches the image in the profile update. Only delete if they are different, in other words only if a new image was specified.

    Thread Starter pixel016

    (@pixel016)

    Yes but how to know that ?

    Moderator bcworkz

    (@bcworkz)

    I don’t know what the rest of your code looks like, so this may be off some, but the general idea would be to do this instead of a simple else:
    elseif ( WP_CONTENT_DIR . '/uploads' . $filename != wp_get_attachment_thumb_file( $post_id )) {

    Thread Starter pixel016

    (@pixel016)

    This is my code

    $wp_filetype = wp_check_filetype(basename(WP_CONTENT_DIR . '/uploads' . $filename), null );
                    $wp_upload_dir = wp_upload_dir();
                    $attachment = array(
                       'guid' => $wp_upload_dir['url'] . '/' . basename( WP_CONTENT_DIR . '/uploads' . $filename ),
                       'post_mime_type' => $wp_filetype['type'],
                       'post_title' => $userNickname,
                       'post_content' => $userDescription,
                       'post_status' => 'inherit'
                    );
    
            $thumbnail_id = get_post_thumbnail_id( $post_ID );
            //error_log('thumbnail id : '.$thumbnail_id);
            error_log(wp_get_attachment_thumb_file( $post_ID ));
    
            if ($thumbnail_id == "") { // Si une image existe pas alors on l'ajoute
                  error_log('ajout nouvelle image id : '.$thumbnail_id);
                  $attach_id = wp_insert_attachment( $attachment, WP_CONTENT_DIR . '/uploads' . $filename, $post_ID);
                  require_once( ABSPATH . 'wp-admin/includes/image.php' );
                  $attach_data = wp_generate_attachment_metadata( $attach_id, WP_CONTENT_DIR . '/uploads' . $filename );
                  wp_update_attachment_metadata( $attach_id, $attach_data );
                  set_post_thumbnail( $post_ID, $attach_id );
            }
            elseif ( WP_CONTENT_DIR . '/uploads' . $filename != wp_get_attachment_thumb_file( $post_ID )) { // Sinon on la remplace
                  error_log('remplacement image id : '.$thumbnail_id);
                  wp_delete_attachment($thumbnail_id );
                  $attach_id = wp_insert_attachment( $attachment, WP_CONTENT_DIR . '/uploads' . $filename, $post_ID);
                  require_once( ABSPATH . 'wp-admin/includes/image.php' );
                  $attach_data = wp_generate_attachment_metadata( $attach_id, WP_CONTENT_DIR . '/uploads' . $filename );
                  wp_update_attachment_metadata( $attach_id, $attach_data );
                  set_post_thumbnail( $post_ID, $attach_id );
            }

    But the condition is not fullfilled.

    Moderator bcworkz

    (@bcworkz)

    Well, my suggestion was only a guess. You’ll need to do some debugging to find the right conditions to check so that the two values are the same except when someone uploads a replacement image.

    I see you’ve made some attempts. Is wp_get_attachment_thumb_file() providing the correct image path? If so, you need to find someway of matching that from the form data when nothing has changed and not matching if a replacement image is specified.

    Is the form showing the current image path when it loads? It needs to in order to know when it has been changed.

    Thread Starter pixel016

    (@pixel016)

    My problem is how to get the url of the new picture uploaded because i get it like this :

    $filename = get_user_meta($current_user->ID, ‘image_profil’,true);

    And my fonction is executed in the ‘profile_update’ hook.

    Moderator bcworkz

    (@bcworkz)

    If the image filename is in usermeta, it should have already been uploaded. Do you mean the problem is how to upload the new image as part of the ‘profile_update’ callback? How is the initial image uploaded? I’m not sure what you’re looking for at this point. Let’s review the basic logic.

    • The form loads, getting the stored filename value in usermeta for the image field. Initially, there is no value in usermeta, so the image field is blank.
    • If the user selects a new file for the field, the form value of course does not match the non-existent usermeta value. The file is uploaded, attachment created, and the new server filename placed in usermeta.
    • Now when the form loads, there is a value in usermeta, so it is displayed in the image field.
    • If the user does nothing with the image field, the field value matches that in usermeta, so nothing is done related to this value.
    • If the user selects a new file for the field, the form value does not match the one in usermeta. The old attachment is deleted. The file is uploaded, attachment created, and the new server filename placed in usermeta.

    Is this correct? Exactly what part are you having trouble with? Note that in a sense, the last situation is identical to the second except the old attachment must be deleted.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to update image’ is closed to new replies.