Support » Fixing WordPress » wp-comments-post.php not returning correct URL after comment post

  • stephengunn

    (@stephengunn)



    Hey guys I’ve been trying to troubleshoot this issue for about a week and I am having a hell of a time so any help would be greatly appreciated. I am working on two sites installed on the same multi-site, site A functions properly. Site B does not. Unfortunately, default comments on both sites are integral to their functionality and I need them to be working properly. My WordPress is fully up to date and I haven’t edited any core files. I am using one parent-theme with two child-themes setup. I have created blank test sites with the default theme and still got the error. I also did full conflict testing where I cloned the working site- same error, disabled every plugin- same error, created a new blank site with no plugins active- same error. But still site A seems to work and it’s running off of the same core of the sites that don’t work.

    Here is a description of the problem:

    When you’re logged in and you post a reply to a post / custom post type the comment is posted/registered but the user is redirected to the blog’s root URL with this at the end: #comment-

    So the site redirects to this: http://www.siteurl.com/#comment- after posting comments. I just need it to redirect back to the post.

    I’ve tracked the redirect process of commenting to /rootdir/wp-comments-post.php and on line 50 this happens:

    $location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment-comment_ID;

    I have tried installing Ajax comment loading scripts/plugins but none of them worked. I have also tried using Filters inside of my functions.php file but I couldn’t get the right code. Here is the one I tried:

    add_filter('comment_post_redirect', 'redirect_after_comment');
    function redirect_after_comment($location)
    {
    
    	$location = get_permalink();
    
    	return $location;
    }

    This just landed me on a blank wp-comments-post.php after commenting.

    And that’s where I am now.. I think if I could just get that comment redirect filter in my functions.php working I would be fine. I just need to wind up back at the post and don’t need the exact comment permalink.

    Any help / discussion is greatly appreciated, I have done every google search I can think of with no luck.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    One way I can think of would to string replace that.

    Something like:

    
    add_filter( 'comment_post_redirect', 'redirect_after_comment', 10, 2 );
    function redirect_after_comment($location, $comment )
    {
    	$location = str_replace( "#comment-{$comment->comment_ID}", '', $location );
    
    	return $location;
    }
    

    I haven’t tried that but you sort of get the idea. 🙂

    stephengunn

    (@stephengunn)

    Just tested it and unfortunately it just loads http://siteurl.com/#comment-

    this problem is beyond frustrating 🙁

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    That is odd. I tried this

    
    add_filter('comment_post_redirect', function($location, $comment) {
      return str_replace("#comment-{$comment->comment_ID}", '', $location );
    }, 10, 2 );
    

    on my random test site and it removes it.

    stephengunn

    (@stephengunn)

    Ok so this time it just redirected me to my root URL with no #comment- on the end.

    I talked to someone who knows WordPress at my hosting company and they said everything is setup correctly when it comes to server. I am using PHP 5.7. This is what my .htaccess file looks like: https://pastebin.com/jyJQvUh7

    The weirdest thing is, the redirect works on Site A but not Site B. I can clone Site A and the redirect does not work on the clone either.

    Is there any way to debug what is happening? I don’t know what else to try, and it doesn’t seem like anyone else has this problem so there isn’t much online about it.

    It just seems like the wp-comment-post.php cannot read the location of the post. Neither can the work around.

    • This reply was modified 1 month ago by  stephengunn.
    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Okay, instead of trying to force it to redirect properly, figure out why it doesn’t redirect properly in the first place. Because this stuff works out-of-the-box, with no issues. I use multisite too, and comments redirect to where they are supposed to just fine.

    So, take your add-on code, remove it, test with it all gone. Then let’s try to figure out the underlying cause. Basically, it should work without any changes, so figuring out what is breaking it is going to work better than to trying to work around it somehow.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Also, there is no such thing as “PHP 5.7”. So, you might want to double check that.

    stephengunn

    (@stephengunn)

    Also, there is no such thing as “PHP 5.7”. So, you might want to double check that.

    You are correct, I mean’t to type 5.6. I know this should work out of the box, Site A works just fine but when I clone it to test it, it breaks on comment.

    I just set my PHP version to 7 to test it and deleted the provided code from my functions.php and the problem persists just like in my original post.

    Every other functionality of WP is working just fine. When I cloned the working site and did conflict testing on it, it never worked properly. (comments only)

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Okay, so if you have an out-of-the-box test site that fails, we need to see that. We need to see that failure case and look at the HTML code ourselves, to see how it fails and why.

    Links to live failures would be helpful.

    stephengunn

    (@stephengunn)

    Ok, it only fails when logged in. The site that woks is http://www.marygunnfunn.com and the site that I am working on is a premium membership site that is managed by WPMUDEV’s Membership 2 plugin located at http://www.funnuniversity.com. Members on the University site earn points by commenting / interacting with the site, so that is why I am really trying to get this functioning smoothly.

    I don’t know how you want to do the testing. I really don’t think it’s a plugin conflict since I have done full testing w/ default theme and every single plugin turned off.

    Here is my wp-comments-post.php: https://pastebin.com/rj31reRU

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.