• Share Tốt – Chia Sẻ Kinh Nghiệm Tồi Tệ
Share Tốt
Liên hệ quảng cáo 0965 21 8696
  • Trang chủ
  • WordPress
  • Share Code
  • Dịch vụ Web
  • 365 Ngày 24/7
  • Thủ Thuật
  • Lập Trình PHP
  • News CNTT
  • SEO Google
No Result
View All Result
  • Trang chủ
  • WordPress
  • Share Code
  • Dịch vụ Web
  • 365 Ngày 24/7
  • Thủ Thuật
  • Lập Trình PHP
  • News CNTT
  • SEO Google
No Result
View All Result
Share Tốt
No Result
View All Result

Trang chủ Wordpress

Share code upload tải ảnh multiple file WordPress

0
SHARES
31
VIEWS
Share on FacebookShare on Twitter

Form khung upload file gắn vào phần đăng bài viết.

<input type=”file” name=”upload_attachment[]” class=”files” size=”50″ multiple=”multiple”>

wp_nonce_field( 'upload_attachment', 'my_image_upload_nonce' );

Rồi bạn sử dụng code sau gắn vào phần đăng bài PHP

if ($_FILES) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

$files = $_FILES['upload_attachment'];
$count = 0;
$galleryImages = array();

foreach ($files['name'] as $count =&gt; $value) {

if ($files['name'][$count]) {

$file = array(
'name' => $files['name'][$count],
'type' => $files['type'][$count],
'tmp_name' => $files['tmp_name'][$count],
'error' => $files['error'][$count],
'size' => $files['size'][$count]
);

$upload_overrides = array( 'test_form' =&gt; false );
$upload = wp_handle_upload($file, $upload_overrides);

// $filename should be the path to a file in the upload directory.
$filename = $upload['file'];

// The ID of the post this attachment is for.
$parent_post_id = $post_id;

// Check the type of tile. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);

// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );

array_push($galleryImages, $attach_id);

}

$count++;

// add images to the gallery field
update_field('field_535e6a644107b', $galleryImages, $post_id);

}

}

Chúc các bạn thành công.

Đám mây từ khóa

365 Ngày 24/7 Design Dịch vụ Web Lập Trình PHP News CNTT SEO Google Share Code Thủ Thuật Wordpress
ADVERTISEMENT

Tin tức gần đây

Top 5 công ty thiết kế website chuyên nghiệp uy tín TPHCM

#TOP Công ty dịch vụ thiết kế APP uy tín ở tại Hải Phòng

30 Tháng Mười, 2020
Gắn nút Hotline – Zalo – Facebook cho Website năm 2020

Gắn nút Hotline – Zalo – Facebook cho Website năm 2020

14 Tháng Mười Một, 2020
Lỗi đầy nội dung cơ sở dữ liệu MySql ” fatal error: allowed memory size of “

Lỗi đầy nội dung cơ sở dữ liệu MySql ” fatal error: allowed memory size of “

15 Tháng Mười, 2020
STREAM ĐẾN BAO GIỜ – ĐỘ MIXI TOP 1 Thịnh Hành sau 2 ngày

STREAM ĐẾN BAO GIỜ – ĐỘ MIXI TOP 1 Thịnh Hành sau 2 ngày

15 Tháng Chín, 2020

Bài viết liên quan

Gắn nút Hotline – Zalo – Facebook cho Website năm 2020
Share Code

Gắn nút Hotline – Zalo – Facebook cho Website năm 2020

14 Tháng Mười Một, 2020
Lỗi đầy nội dung cơ sở dữ liệu MySql ” fatal error: allowed memory size of “
Wordpress

Lỗi đầy nội dung cơ sở dữ liệu MySql ” fatal error: allowed memory size of “

15 Tháng Mười, 2020
Cách code đưa nút Add Media WordPress Custom
Share Code

Cách code đưa nút Add Media WordPress Custom

27 Tháng Bảy, 2020
Code thêm tab cạnh mô tả cho woocommerce
Wordpress

Code thêm tab cạnh mô tả cho woocommerce

20 Tháng Năm, 2020
Loại bỏ trong đường dẫn danh mục sản phẩm Woocommerce
Wordpress

Thay đổi thẻ html woocommerce change product title p to h3

12 Tháng Sáu, 2020

Bài tiếp theo

Code thay đổi tác giả bài viết cho Thành viên đăng ký ( subscriber ) Wordpress

Share Tốt

Share Tốt chia sẻ kinh nghiệm, tin tức, thủ thuật, bài giảng hay cho mọi lứa tuổi xem trên trang.

Theo dõi tôi

Chuyên Mục

  • 365 Ngày 24/7
  • Design
  • Dịch vụ Web
  • Lập Trình PHP
  • News CNTT
  • SEO Google
  • Share Code
  • Thủ Thuật
  • Wordpress

Bài viết mới

Top 5 công ty thiết kế website chuyên nghiệp uy tín TPHCM

#TOP Công ty dịch vụ thiết kế APP uy tín ở tại Hải Phòng

30 Tháng Mười, 2020
Gắn nút Hotline – Zalo – Facebook cho Website năm 2020

Gắn nút Hotline – Zalo – Facebook cho Website năm 2020

14 Tháng Mười Một, 2020
  • Share Tốt – Chia Sẻ Kinh Nghiệm Tồi Tệ

© 2020 Share Tốt - Thiết kế web Hải Phòng.

No Result
View All Result
  • Trang chủ
  • WordPress
  • Share Code
  • Dịch vụ Web
  • 365 Ngày 24/7
  • Thủ Thuật
  • Lập Trình PHP
  • News CNTT
  • SEO Google

© 2020 Share Tốt - Thiết kế web Hải Phòng.