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.
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> Thay https://lovemama.vn/pdf/bang-gia.pdf: thành file pdf của em