You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress中获取文件夹文件并在后台自动展示下载锚点

Hey there! As a fellow WordPress beginner who’s tackled similar problems, I’ve got a straightforward solution for you. Let’s walk through how to set this up so your download links auto-update in the dashboard whenever you add new files.

Step 1: Pick Your Target Folder

First, decide where you want to store your download files. I recommend creating a subfolder inside WordPress’s default uploads directory (this avoids permission headaches).

Create a folder named custom-downloads inside wp-content/uploads/ (you can rename this to whatever you want—just remember to update the code later).

Step 2: Add the Code to Your WordPress Site

You’ll need to add a custom dashboard widget that reads your folder and generates links. You can put this code in either:

  • Your child theme’s functions.php file (best practice to avoid losing changes when your parent theme updates)
  • A custom plugin (even better for portability)

Here’s the code—copy and paste it, then adjust the folder name if you changed it:

// Register our custom dashboard widget
add_action('wp_dashboard_setup', 'my_custom_downloads_widget');
function my_custom_downloads_widget() {
    wp_add_dashboard_widget(
        'custom_downloads_dashboard_widget', // Unique slug for the widget
        'My Download Links',                  // Title that shows in the dashboard
        'render_custom_downloads_links'       // Function that outputs the links
    );
}

// Function to render the download links in the widget
function render_custom_downloads_links() {
    // Get WordPress's upload directory details
    $upload_dir = wp_upload_dir();
    
    // Define your target folder (update this if you renamed your folder)
    $download_folder_path = $upload_dir['basedir'] . '/custom-downloads/';
    $download_folder_url = $upload_dir['baseurl'] . '/custom-downloads/';

    // Check if the folder exists—if not, show an error
    if (!file_exists($download_folder_path)) {
        echo '<p>Oops! The download folder doesn\'t exist yet. Create it first at <code>wp-content/uploads/custom-downloads/</code>.</p>';
        return;
    }

    // Read all items in the folder
    $folder_items = scandir($download_folder_path);
    // Filter out the default . and .. directory entries
    $files = array_diff($folder_items, array('.', '..'));

    // If no files are found, let the user know
    if (empty($files)) {
        echo '<p>No files found in your download folder yet.</p>';
        return;
    }

    // Generate the list of download links
    echo '<ul style="margin-left: 20px; list-style-type: disc;">';
    foreach ($files as $file) {
        $full_file_path = $download_folder_path . $file;
        $full_file_url = $download_folder_url . $file;

        // Only display links for actual files (not subfolders)
        if (is_file($full_file_path)) {
            // Use esc_url and esc_html to keep things secure
            echo '<li><a href="' . esc_url($full_file_url) . '" target="_blank" download>' . esc_html($file) . '</a></li>';
        }
    }
    echo '</ul>';
}
Step 3: Customize (Optional)

Want to tweak this further? Here are some quick adjustments you can make:

  • Restrict file types: If you only want to show PDFs, ZIPs, etc., add this check inside the foreach loop:
    // Allow only specific file extensions
    $allowed_extensions = array('pdf', 'zip', 'docx');
    $file_extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    
    if (is_file($full_file_path) && in_array($file_extension, $allowed_extensions)) {
        // Generate the link here
    }
    
  • Change the widget title: Update the 'My Download Links' text in the wp_add_dashboard_widget function.
  • Style the widget: Add custom CSS to the <ul> or <li> tags to match your dashboard’s look.
Step 4: Test It Out
  1. Upload a file to your custom-downloads folder (via FTP or your file manager).
  2. Refresh your WordPress dashboard—you should see the new widget with your download link!
  3. Add another file, refresh again, and it’ll automatically show up.

Important Notes

  • Permissions: Make sure your custom-downloads folder has the right permissions (usually 755 for folders, 644 for files) so WordPress can read it.
  • Security: The code uses esc_url() and esc_html() to prevent malicious code from being injected—never skip these!

Give this a go, and if you hit any issues (like permission errors or links not showing up), feel free to ask for more help.

内容的提问来源于stack exchange,提问作者Huzail Jamil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:07:40