WordPress自定义评论提交后重定向至指定页面问题求助
Let's break down why this is happening and walk through the fixes step by step:
Root Cause Analysis
The redirect to wp-login.php is almost always triggered by WordPress security checks failing or missing critical logic from the original wp-comment-post.php file. Your setup is missing two key components: a security nonce field in the form, and proper validation/logic in your custom wp-comments.php handler.
Step 1: Add a Security Nonce to Your Comment Form
WordPress requires a nonce (a security token) to verify that the comment submission is legitimate. Your form doesn't include this, so WordPress blocks the request and redirects to the login page as a safety measure.
Add this hidden field to your form, right after the comment_post_ID hidden input:
<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'comment_post' ); ?>" />
This generates a valid security token that your custom handler will need to validate.
Step 2: Fix Your Custom wp-comments.php Handler
Your custom file is missing critical logic from the original wp-comment-post.php. Make sure it includes these key parts:
1. Load the WordPress Environment
At the very top of wp-comments.php, add this to ensure WordPress core functions are available:
require_once( dirname(__FILE__) . '/wp-load.php' );
2. Validate the Nonce
Before processing the comment, add this to verify the security token:
check_admin_referer( 'comment_post', '_wpnonce' );
If the nonce is invalid, this will stop execution and prevent unauthorized requests.
3. Fix the Redirect URL Handling
Your current redirect logic might not properly resolve relative URLs. Update it to ensure the redirect path is a valid absolute URL:
// Get the redirect URL from the form $redirect_to = isset( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : ''; if ( !empty( $redirect_to ) ) { // Convert relative path to absolute URL using WordPress home URL $redirect_url = home_url( $redirect_to ); // Append the comment anchor to jump to the new comment $location = $redirect_url . '#comment-' . $comment->comment_ID; } else { // Fallback to the default comment link if no redirect is specified $location = get_comment_link( $comment ); } // Apply WordPress filters and redirect $location = apply_filters( 'comment_post_redirect', $location, $comment ); wp_safe_redirect( $location ); exit;
4. Ensure Visitor Comment Logic is Intact
Make sure you retained the original logic for handling guest comments (since you're allowing non-logged-in users to comment):
- Checks for required
authorandemailfields - Validates email format
- Handles comment moderation (if your site uses it)
If these checks are missing, WordPress might treat the request as invalid and redirect to login.
Step 3: Verify WordPress Discussion Settings
Double-check your site's comment settings to ensure guest commenting is enabled:
- Go to Settings > Discussion in your WordPress admin
- Uncheck the option that says "Users must be registered and logged in to comment"
If this is checked, WordPress will automatically redirect non-logged-in users to the login page when they try to submit a comment.
Final Checks
- Ensure your
wp-comments.phpfile has the same file permissions aswp-comment-post.php(usually644) - Test the form again after making these changes—you should now redirect to your specified page with the new comment anchored.
内容的提问来源于stack exchange,提问作者Ninder Chand

