如何在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.
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).
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.phpfile (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>'; }
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
foreachloop:// 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 thewp_add_dashboard_widgetfunction. - Style the widget: Add custom CSS to the
<ul>or<li>tags to match your dashboard’s look.
- Upload a file to your
custom-downloadsfolder (via FTP or your file manager). - Refresh your WordPress dashboard—you should see the new widget with your download link!
- Add another file, refresh again, and it’ll automatically show up.
Important Notes
- Permissions: Make sure your
custom-downloadsfolder has the right permissions (usually755for folders,644for files) so WordPress can read it. - Security: The code uses
esc_url()andesc_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

