Hướng dẫn code function trang Chuyển đổi các liên kết chia sẻ Google Drive thành liên kết tải trực tiếp
Thêm mã PHP vào file functions.php
Chúng ta sẽ tạo một shortcode để chèn đoạn mã vào bất kỳ trang nào.
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
// Thêm Shortcode cho Google Drive Direct Download Link Generator function gdrive_direct_download_shortcode() { ob_start(); // Bắt đầu buffer output để chèn mã HTML/JS ?> <div class="gdrive-link-generator"> <h1>Google Drive Direct Download Link Generator</h1> <textarea id="shareLinks" rows="6" cols="60" placeholder="Nhập nhiều link Google Drive share, mỗi link 1 dòng"></textarea> <button onclick="generateDownloadLinks()">Generate Direct Download Links</button> <div class="result" id="result"></div> <button id="copyAllBtn" onclick="copyAllLinks()" style="display: none;">Copy All Links</button> </div> <style> .gdrive-link-generator { font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; background-color: #f4f4f9; padding: 20px; border: 1px solid #ccc; border-radius: 10px; max-width: 800px; margin: auto; } textarea, button { padding: 10px; margin: 5px; font-size: 16px; } .result { margin-top: 20px; padding: 10px; border: 1px solid #ccc; background-color: #e4e4e4; width: 100%; max-width: 600px; } .result-item { display: flex; align-items: center; margin-bottom: 10px; } .copy-btn { margin-left: 10px; padding: 5px 10px; font-size: 14px; cursor: pointer; } </style> <script> let allLinks = []; function generateDownloadLinks() { const shareLinks = document.getElementById("shareLinks").value; const resultDiv = document.getElementById("result"); const copyAllBtn = document.getElementById("copyAllBtn"); if (!shareLinks.trim()) { resultDiv.innerHTML = "Vui lòng nhập một hoặc nhiều liên kết Google Drive!"; copyAllBtn.style.display = "none"; return; } const linksArray = shareLinks.split('\n').map(link => link.trim()).filter(link => link !== ""); resultDiv.innerHTML = ""; // Xóa kết quả trước đó allLinks = []; // Xóa danh sách liên kết trước đó linksArray.forEach((link, index) => { const fileId = extractFileId(link); const resultItem = document.createElement("div"); resultItem.classList.add("result-item"); if (fileId) { const directDownloadLink = `https://drive.google.com/uc?export=download&id=${fileId}`; resultItem.innerHTML = ` <strong>Link ${index + 1}:</strong> <a href="${directDownloadLink}" target="_blank">${directDownloadLink}</a> <button class="copy-btn" onclick="copyToClipboard('${directDownloadLink}')">Copy</button> `; allLinks.push(directDownloadLink); } else { resultItem.innerHTML = `<strong>Link ${index + 1}:</strong> Liên kết Google Drive không hợp lệ!`; } resultDiv.appendChild(resultItem); }); if (allLinks.length > 0) { copyAllBtn.style.display = "block"; } else { copyAllBtn.style.display = "none"; } } function extractFileId(url) { const regex = /[-\w]{25,}/; const match = url.match(regex); return match ? match[0] : null; } function copyToClipboard(text) { const tempInput = document.createElement("input"); document.body.appendChild(tempInput); tempInput.value = text; tempInput.select(); document.execCommand("copy"); document.body.removeChild(tempInput); alert("Đã sao chép vào clipboard: " + text); } function copyAllLinks() { const allLinksText = allLinks.join('\n'); const tempInput = document.createElement("textarea"); document.body.appendChild(tempInput); tempInput.value = allLinksText; tempInput.select(); document.execCommand("copy"); document.body.removeChild(tempInput); alert("Tất cả liên kết đã được sao chép vào clipboard!"); } </script> <?php return ob_get_clean(); // Trả về HTML/JS dưới dạng chuỗi } add_shortcode('gdrive_link_generator', 'gdrive_direct_download_shortcode'); //---------- |
[=.=
vào nơi muốn hiển thị công cụ
]
Quý khách cần tư vấn thiết kế web giá rẻ, vui lòng nhấn nút bên dưới:
Chào ! Bạn thấy nội dung này thế nào?
Xem thêm:
- Ichiban Logistic – Không ngừng cải tiến để phục vụ
- Một số lệnh cơ bản của Rclone
- 28 tiêu chí SEO Onpage quan trọng nhất được Google công nhận hoặc khuyến nghị
- Hướng dẫn tạo hiệu ứng “Xem thêm / Thu gọn” cho phần mô tả sản phẩm
- Hiệu ứng chiếu sáng (shine effect) (hiệu ứng ánh sáng lướt qua) khi hover vào hình ảnh