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 badges1 Answer
Reset to default 0The cause was WPML settings which dupplicates media. :/
本文标签: imageswpinsertattachment() dupplicate attachment posts
版权声明:本文标题:images - wp_insert_attachment() dupplicate attachment posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742327080a2453940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论