WordPress首个插件问题求助:后台表单隐藏与短码功能修复
Fixing Your Zach First Plugin Issues
Got it, let's sort out those two problems with your WordPress plugin right away. Here's the step-by-step solution with modified code:
Problem 1: Hide Upload Form from Admin Menu
The current test_init function outputs the upload form directly when you click the admin menu. We'll replace that form with a simple message telling users to use the shortcode on the frontend instead.
Problem 2: Make Shortcode Display Upload Form
Right now your shortcode just returns "Hello World!"—we'll update it to render the upload form, and also ensure the file upload handling works from the frontend.
Modified Full Code
<?php /* Plugin Name: Zach First Plugin Description: Plugin to create a menu in WP Author: Zach Version: 0.1 */ // Call function to create an Admin menu element add_action('admin_menu', 'test_plugin_setup_menu'); function test_plugin_setup_menu(){ add_menu_page( 'Zach Plugin Page', 'Zach Plugin', 'manage_options', 'test-plugin', 'test_init' ); } // Admin menu page content (no upload form here anymore) function test_init(){ ?> <h1>Zach First Plugin</h1> <p>Use the <code>[pdf]</code> shortcode on any post or page to access the file upload form on the frontend.</p> <?php } // Handle file uploads (moved to init hook so it works on frontend too) add_action('init', 'test_handle_post'); function test_handle_post(){ // Only process if it's a POST request and the file is set if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['test_upload_pdf'])){ // Check user capability to upload files if(!current_user_can('upload_files')){ wp_die('You do not have permission to upload files.'); } $pdf = $_FILES['test_upload_pdf']; // Use WordPress media upload function $uploaded = media_handle_upload('test_upload_pdf', 0); // Error checking if(is_wp_error($uploaded)){ echo "<p style='color:red;'>Error uploading file: " . $uploaded->get_error_message() . "</p>"; }else{ echo "<p style='color:green;'>File upload successful! <a href='" . wp_get_attachment_url($uploaded) . "' target='_blank'>View File</a></p>"; } } } // Shortcode to display upload form on frontend add_shortcode("pdf", "test_process_shortcode"); function test_process_shortcode(){ // Check if user has permission to upload if(!current_user_can('upload_files')){ return "<p>You do not have permission to upload files.</p>"; } ob_start(); // Start output buffering to capture HTML ?> <h2>Upload a PDF File</h2> <!-- Upload form with proper enctype --> <form method="post" enctype="multipart/form-data"> <input type='file' id='test_upload_pdf' name='test_upload_pdf' accept='application/pdf' required></input> <?php submit_button('Upload PDF') ?> </form> <?php return ob_get_clean(); // Return the captured HTML } ?>
Key Changes Explained
- Admin Menu Cleanup: The
test_initfunction now only shows a header and instructions instead of the upload form. - Upload Handling Hook: Moved
test_handle_postto theinithook so it triggers both on admin and frontend requests (critical for processing frontend form submissions). - Shortcode Form: Updated
test_process_shortcodeto render the upload form using output buffering (better practice for generating HTML in shortcodes). Also added:- Permission check to ensure only users with upload rights can see the form.
accept='application/pdf'to restrict uploads to PDF files.requiredattribute to enforce file selection before submission.- Success/error messages with styling for better user feedback.
These changes will:
- Remove the upload form from your admin menu page
- Make the
[pdf]shortcode display the upload form on frontend posts/pages - Ensure file uploads work correctly from the frontend with proper error handling
内容的提问来源于stack exchange,提问作者TSCAmerica.com
相关产品推荐
相关产品推荐

