admin管理员组

文章数量:1332345

I made a function to add an image in WP and attach it to the right post. The specificity is the input is an url to get the picture. My problem is when wp_insert_attachment() is run I got a dupplicate in Medias and the second one is not attached.

My function

function save_media_from_url( $image_url, $post_content = "", $post_id = null ){
   $upload_dir = wp_upload_dir();

   $image_data = file_get_contents( $image_url );

   $filename = basename( $image_url );

   if ( wp_mkdir_p( $upload_dir['path'] ) ) {
       $file = $upload_dir['path'] . '/' . $filename;
   }
   else {
       $file = $upload_dir['basedir'] . '/' . $filename;
   }

   file_put_contents( $file, $image_data );

   $wp_filetype = wp_check_filetype( $filename, null );

   $attachment = array(
       'post_mime_type' => $wp_filetype['type'],
       'post_title' => sanitize_file_name( $filename ),
       'post_content' => $post_content,
       'post_status' => 'inherit'
   );

   $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
   var_dump( $attach_id );
   wp_die();
   require_once( ABSPATH . 'wp-admin/includes/image.php' );
   $attach_data = wp_generate_attachment_metadata( $attach_id, $file ) 
   wp_update_attachment_metadata( $attach_id, $attach_data );
   return $attach_id;

}

OUTPUT FOR DEBUGGING

int(188880) <-- attachment id for the first saved ( linked to the right post )

The image is saved two times. The first record in database is linked to the right post and this is the return id but there is a second one saved wihtout any post linked.

Someone know how to fixe this ?

I made a function to add an image in WP and attach it to the right post. The specificity is the input is an url to get the picture. My problem is when wp_insert_attachment() is run I got a dupplicate in Medias and the second one is not attached.

My function

function save_media_from_url( $image_url, $post_content = "", $post_id = null ){
   $upload_dir = wp_upload_dir();

   $image_data = file_get_contents( $image_url );

   $filename = basename( $image_url );

   if ( wp_mkdir_p( $upload_dir['path'] ) ) {
       $file = $upload_dir['path'] . '/' . $filename;
   }
   else {
       $file = $upload_dir['basedir'] . '/' . $filename;
   }

   file_put_contents( $file, $image_data );

   $wp_filetype = wp_check_filetype( $filename, null );

   $attachment = array(
       'post_mime_type' => $wp_filetype['type'],
       'post_title' => sanitize_file_name( $filename ),
       'post_content' => $post_content,
       'post_status' => 'inherit'
   );

   $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
   var_dump( $attach_id );
   wp_die();
   require_once( ABSPATH . 'wp-admin/includes/image.php' );
   $attach_data = wp_generate_attachment_metadata( $attach_id, $file ) 
   wp_update_attachment_metadata( $attach_id, $attach_data );
   return $attach_id;

}

OUTPUT FOR DEBUGGING

int(188880) <-- attachment id for the first saved ( linked to the right post )

The image is saved two times. The first record in database is linked to the right post and this is the return id but there is a second one saved wihtout any post linked.

Someone know how to fixe this ?

Share Improve this question edited Jun 22, 2020 at 17:45 J.BizMai asked Jun 22, 2020 at 17:32 J.BizMaiJ.BizMai 9002 gold badges10 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The cause was WPML settings which dupplicates media. :/

本文标签: imageswpinsertattachment() dupplicate attachment posts