Code download pdf wordpress in Function.php như thế nào?

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

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,030
    Đã được thích:
    81
    Điểm thành tích:
    48
    Cho em xin cái code download file PDF trong worpdress sử dụng function.php với.

    Hiện tại bên em đang cần, mỗi khi khách ấn vào nút tải thì sẽ tải được file PDF ở cái link tải đó về máy.

    Em tham khảo nhiều plugin mà nó nặng quá, xài có 1 cái tải mà chèn cả đống code khá bất tiện. Có code tải file pdf nào php viết trong function.php đơn giản nhẹ không giúp em.
     
    Cảm ơn đã xem bài:

    Code download pdf wordpress in Function.php như thế nào?

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,908
    Đã được thích:
    1,200
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Code gon nhẹ sau nhé, mở function.php của themes lên và chèn code sau vào:
    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 invoice basename
            $filerequest = $_REQUEST['pdfdn'];
            $filerequest = str_replace("https://lovemama.vn","", $filerequest);
            $filepdf = realpath($_SERVER["DOCUMENT_ROOT"].$filerequest);
            if(file_exists($filepdf)) {
                status_header( 200 );
                $filename = basename($filepdf);
                $filetype = filetype($filepdf);
                header('Content-Disposition: attachment; filename=' . $filename);
                header("Content-type: " . $filetype);
                ob_clean();
                flush();
                readfile($filepdf);
                die();//bat buoc phai co khi ket thuc
            }
        }
    }
    
    • Thay lovemama.vn thành url web của bạn
    Vào vị trí cần tải thì cho code html này vào:
    Mã:
    <a href="?pdfdn=https://lovemama.vn/pdf/bang-gia.pdf">Tải file PDF</a>
    
     
    Chỉnh sửa cuối: 12/12/23


Chủ để tương tự : Code download
Diễn đàn Tiêu đề Date
Mã nguồn mở Wordpress Code download file .pdf in function.php wordpress? 30/10/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