Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Upload Article Feature image.
- *
- * @param int $post_id Article ID.
- *
- * @param string $image_name image name.
- *
- * @param string $full_image_path Full image path from where image will upload.
- */
- function ie_upload_feature_image( $post_id, $image_name = '', $full_image_path = '' ) {
- if ( ! empty( $post_id ) && ! empty( $image_name ) && ! empty( $full_image_path ) ) {
- $upload = wp_upload_bits( $image_name, null, wpcom_vip_file_get_contents( esc_url_raw( $full_image_path ), 3, 60 ) );
- if ( is_array( $upload ) && array_key_exists( 'file', $upload ) ) {
- $filename = $upload['file'];
- $file_type = wp_check_filetype( $filename, null ); // Retrieve the file type from the file name.
- if ( is_array( $file_type ) && array_key_exists( 'type', $file_type ) ) {
- $attachment = array(
- 'post_mime_type' => $file_type['type'],
- 'post_title' => sanitize_file_name( $filename ),
- 'post_content' => '',
- 'post_status' => 'inherit',
- );
- $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
- if ( ! empty( $attach_id ) ) {
- require_once trailingslashit( ABSPATH ) . 'wp-admin/includes/image.php';
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- set_post_thumbnail( $post_id, $attach_id );
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement