Home › Forums › Plugin Support › File upload field > update image in wordpress library
Tagged: custom field, image upload, Upload field
- This topic has 5 replies, 2 voices, and was last updated 10 years, 9 months ago by pixel016.
Viewing 6 posts - 1 through 6 (of 6 total)
-
AuthorPosts
-
February 19, 2014 at 2:35 pm #4863February 21, 2014 at 4:40 pm #4876pixel016Member
Nobody can help me ?
February 26, 2014 at 1:41 pm #4917pixel016MemberThe full code :
$filename = get_user_meta($current_user->ID, 'image_profil',true); $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 ); 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 ); } else { // 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 ); }
February 27, 2014 at 12:13 am #4925KhaledMemberHello,
If your image_profil meta_key is using by UMP, its value already contain /uploads/
Checkout those code:
global $userMeta; $filename = get_user_meta( $current_user->ID, 'image_profil', true ); $file = $userMeta->determinFileDir( $filename ); $filepath = $file['path']; $wp_filetype = wp_check_filetype( basename( $filepath ), null );
Change your
WP_CONTENT_DIR . '/uploads' . $filename
to
$filepath
You can access file url by $file[‘url’]
Thanks.
March 3, 2014 at 10:15 am #4927pixel016MemberThanks Khaled, but when i use your code, when i’m logged like user and update my profile picture in the media library a new item is created but empty :s But the image profile update is ok
March 3, 2014 at 10:29 am #4928pixel016MemberI precise the code is executed in ‘profile_update’ hook
-
AuthorPosts
Viewing 6 posts - 1 through 6 (of 6 total)
- You must be logged in to reply to this topic.