images - wp_insert_attachment() dupplicate attachment posts

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 t

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. :/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742327081a4422965.html

相关推荐

  • images - wp_insert_attachment() dupplicate attachment posts

    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 t

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信