Code download file .pdf in function.php wordpress?

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi seolagi, 30/10/23.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,043
    Đã được thích:
    81
    Điểm thành tích:
    48
    Mình đang làm chức năng download trong wordpress cho sản phẩm, mỗi sản phẩm đều có cái nút download ở ngoài trang chi tiết sản phẩm.
    Mình dùng ACF để tạo field cho các bạn tải file PDF lên rồi add vào đó.
    Mình gọi nút download ra ngoài rồi nhưng không biết sao để cho nó tải được.
    Mã:
    <a href="?pdfdn=https://itseovn.com/file/pdf.pdf" id="pdf_download">Tải file PDF hướng dẫn sử dung</a>
    
    Làm sao để mỗi người ấn nút Tải file PDF hướng dẫn sử dụng thì web sẽ tự tải file về cho khách vậy?
     
    Cảm ơn đã xem bài:

    Code download file .pdf in function.php wordpress?

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,969
    Đã được thích:
    1,210
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Bạn add code sau vào function.php của themes nhé.
    Mã:
    add_action( 'init', 'itseovn_show_get_pdf_download' );
    function itseovn_show_get_pdf_download() {
        //Check if invoice is set and get value = the path of the invoice
        if ( isset( $_REQUEST['pdfdn'] ) ) {
            //Get basename
            $filerequest = $_REQUEST['pdfdn'];
            $filerequest = str_replace("https://itseovn.com","", $filerequest);
            $filepdf = realpath($_SERVER["DOCUMENT_ROOT"].$filerequest);
            //file có tồn tại
            if(file_exists($filepdf)) {
                status_header( 200 );
                $basename = basename($filepdf);
                $filesize = filesize($filepdf);
                header('Content-Description: File Transfer');
                header('Content-Type: text/plain');
                header("Cache-Control: no-cache, must-revalidate");
                header("Expires: 0");
                header("Content-Disposition: attachment; filename=$basename");
                header("Content-Length: $filesize");
                header('Pragma: public');
                flush();
                readfile($filepdf);
                die();//bat buoc phai co khi ket thuc
            }
        }
    }
    
     
    Chỉnh sửa cuối: 30/10/23
  3. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,043
    Đã được thích:
    81
    Điểm thành tích:
    48
    Cảm ơn ad, đúng cái em cần, add vào chạy ngay và lun
     


Chủ để tương tự : Code download
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Code download pdf wordpress in Function.php như thế nào? 9/12/23
Mã nguồn mở Wordpress Xin code tự động tạo user trong function.php wordpress 6/10/23
Mã nguồn mở Wordpress Xin code phân trang trong wordpress dễ sử dụng 30/10/22
Mã nguồn mở Wordpress Lấy thứ, ngày, tháng, năm, giờ:phút:giây TizeZone trong code PHP wordpress 2/7/22
Mã nguồn mở Wordpress ShortCode danh sách sản phẩm nổi Featured trong WooCommerce 11/10/21