Я нашел этот код в другом вопросе:
Код: Выделить всё
$filename = "thumbnail_".$post_id".jpg";
$upload_file = wp_upload_bits( $filename, null, @file_get_contents('http://example.com/image.jpg') );
if ( ! $upload_file['error'] ) {
$filename = $upload_file['file'];
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
file_put_contents('attachment_id.txt', print_r($attachment_id, true));
if ( ! is_wp_error( $attachment_id ) ) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
}
}
Но я не не хочу загружать изображение, как мне этого добиться?
Подробнее здесь: https://stackoverflow.com/questions/621 ... -thumbnail