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.

// 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');
//----------

Chèn shortcode [=.=gdrive_link_generator] vào nơi muốn hiển thị công cụ

Chào ! Bạn thấy nội dung này thế nào?

Để lại một bình luận

💬
Trợ lý Webdy
Chatbot được phát triển bởi Webdy.vn