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. :/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742327081a4422965.html
评论列表(0条)