Dowload image from url to media wordpress: vâng code dưới dây sẽ giúp anh chị lấy image từ bất kỳ url nào và lưu vào thư viên media wordpress và trả về id của image để anh chị lưu vào dữ liệu (function get_image_from_url_to_media_wordpress), lưu ý hai function hiện tại đang dùng trong class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
public function get_image_from_url_to_media_wordpress($url){ if(empty($url)){ return false; } $extension_images = array('jpg','png','gif','jpeg'); $ext = pathinfo($url, PATHINFO_EXTENSION); if(!in_array($ext,$extension_images)){ return false; } $overrides = array( 'wp_handle_sideload' => 'upload', ); $file_info = $this->download_file($url, $overrides ); $file_info = $file_info['data']; $file_path = $file_info['file']; $file_type = $file_info['type']; $name =pathinfo($file_path, PATHINFO_FILENAME); //file name without extension $upload_id = wp_insert_attachment( array( 'guid' => $file_path, 'post_mime_type' => $file_type, 'post_title' => preg_replace( '/\.[^.]+$/', '', $name), 'post_content' => '', 'post_status' => 'inherit' ), $file_path); // wp_generate_attachment_metadata() won't work if you do not include this file require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Generate and save the attachment metas into the database wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $file_path) ); //@unlink($file_path); return $upload_id; } public function download_file( $file = '', $overrides = array(), $timeout_seconds = 300 ) { // Gives us access to the download_url() and wp_handle_sideload() functions. require_once ABSPATH . 'wp-admin/includes/file.php'; // Download file to temp dir. $temp_file = download_url( $file, $timeout_seconds ); // WP Error. if ( is_wp_error( $temp_file ) ) { return array( 'success' => false, 'data' => $temp_file->get_error_message(), ); } // Array based on $_FILE as seen in PHP file uploads. $file_args = array( 'name' => basename( $file ), 'tmp_name' => $temp_file, 'error' => 0, 'size' => filesize( $temp_file ), ); $defaults = array( // Tells WordPress to not look for the POST form // fields that would normally be present as // we downloaded the file from a remote server, so there // will be no form fields // Default is true. 'test_form' => false, // Setting this to false lets WordPress allow empty files, not recommended. // Default is true. 'test_size' => true, // A properly uploaded file will pass this test. There should be no reason to override this one. 'test_upload' => true, /*'mimes' => array( 'xml' => 'application/xml', 'json' => 'text/plain', 'zip' => 'application/zip', 'json' => 'application/json' ),*/ ); $overrides = wp_parse_args( $overrides, $defaults ); // Move the temporary file into the uploads directory. $results = wp_handle_sideload( $file_args, $overrides ); //astra_sites_error_log( wp_json_encode( $results ) ); if ( isset( $results['error'] ) ) { return array( 'success' => false, 'data' => $results, ); } // Success. return array( 'success' => true, 'data' => $results, ); } |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress