WordPress插件自定义选项页跳转子页面失败求助
Okay, let's troubleshoot why your 'View' button isn't opening that specific PHP file in your WordPress plugin directory. I've run into this exact issue a few times, so here are the most likely fixes to check step by step:
First off, make sure your button is pointing to the right file. WordPress doesn't play nice with hardcoded paths—you need to use its built-in functions to generate valid URLs.
- If it's a form button, check the
actionattribute:<form method="post" action="<?php echo esc_url( plugin_dir_url( __FILE__ ) . 'your-target-file.php' ); ?>"> - If it's a link button, check the
href:<a href="<?php echo esc_url( plugin_dir_url( __FILE__ ) . 'your-target-file.php' ); ?>" class="button">View</a>
Quick test: Right-click the button, select "Inspect", and look at the generated URL. If it's missing your domain, has a typo in the filename, or points to the wrong folder, that's your problem.
WordPress (and most hosting providers) block direct access to plugin PHP files by default. If you haven't added a security check to your target file, it'll throw a 403 error or load a blank screen.
Add this line at the very top of your target PHP file:
<?php defined( 'ABSPATH' ) || exit; // Prevent direct access ?>
Also, double-check file permissions: set files to 644 and directories to 755. Permissions that are too open or locked down can stop the server from serving the file.
Your current code handles the search when $_POST['search'] is set, but does it actually send the user to the view file after that? If not, the form just submits to the same page instead of opening your target file.
After fetching the user data, use wp_redirect() to send the user to the view file (and pass any needed data like a user ID via query string):
if ( isset( $_POST['search'] ) ) { $search_term = $_POST['search']; // Your existing search logic here... // Grab the user ID you want to view $user_id = $found_user->ID; // Redirect to the view file with the user ID wp_redirect( esc_url( plugin_dir_url( __FILE__ ) . 'view-user.php?user_id=' . $user_id ) ); exit; // Always exit after wp_redirect to stop further code execution }
If your view file needs to load specific user data, make sure it's correctly reading the data you're passing. For example, if you sent a user_id via the URL, sanitize and validate it in the view file:
// Inside your target view file if ( isset( $_GET['user_id'] ) ) { $user_id = absint( $_GET['user_id'] ); // Sanitize the input to prevent issues $user = get_user_by( 'id', $user_id ); if ( ! $user ) { echo '<p>Invalid user selected.</p>'; exit; } } else { echo '<p>No user specified to view.</p>'; exit; }
If you skip this step, the view file might fail to load data, making it seem like the file isn't opening at all.
Double-check that your target PHP file is actually in the same directory as your main plugin file. If you're using subfolders (like /includes/), you need to adjust the path:
// Example if the view file is in an includes subfolder esc_url( plugin_dir_url( __FILE__ ) . 'includes/view-user.php' );
You can verify your current directory by adding this line to your main plugin file (then remove it after testing):
echo plugin_dir_path( __FILE__ );
内容的提问来源于stack exchange,提问作者Shirantha

