Redirects when posting a comment

When posting a comment on one of the subsites as a logged in user, the page redirects to the homepage. However when doing it as a non-logged in user it works fine.

  • Kasia Swiderska

    Hello Stephen,

    I checked your site and checked debug.log but unfortunately there is nothing there that would give us any hints why there is this strange redirect.
    I see in chat transcript it was also tested on subsite without any plugins enabled, however I can see that there are some plugins network activated. Would it be possible to also deactivate those (and also mu-plugins)?
    There is only few of the plugins there so if there is backup of the site done I could quickly test it. Let me know.

    kind regards,

  • Stephen

    So, I talked to my hosting and they had no idea what might be causing my issue.

    I also bought a domain for the site in question and the issue is still there. I have also been doing a lot of searching for fixes that might help me at least half fix my problem.

    I was able to find this code snippet that redirects you back to referrer link after posting a comment:

    add_filter('comment_post_redirect', 'redirect_after_comment');
    function redirect_after_comment($location)
    return $_SERVER["HTTP_REFERER"];

    If I could just replace the return $_SERVER["HTTP_REFERER"]; with a link back to the page where the comment was posted, I would be good to go. I have been messing with it and I can't figure out the right way to write it and my PHP knowledge is limited to copy/paste at best.

    I did find this code but it doesn't work: empty($_POST['redirect_to']) ? get_comment_link($comment_id, array('type' => 'comment')) : $_POST['redirect_to'] . '#comment-' . $comment_id;

    I think I'm pretty close, but if you have any suggestion on how I could just redirect back to the page that the comment was posted, it would be a life saver

  • Kasia Swiderska

    Hello Stephen,

    Can you try to install new WordPress on a subdomain or in a separate directory of that account - manual installation (so not using any auto installers), no plugins, basic theme and see if then something like that is happening?

    At the moment our developer suggested two possible code snippets:

    add_action( 'set_comment_cookies', function( $comment ){
        $_POST['redirect_to'] = get_comment_link( $comment );
    }, 99 );


    add_filter( 'comment_post_redirect', function( $location, $comment ){
        return get_comment_link( $comment );
    }, 99, 2 );

    Let me know if any of those will do the job.

    kind regards,

  • Panos

    Hi Stephen ,

    In the first it shows 2 anchors cause the get_comment_link() adds it once and the wp-comments-post.php file adds it again. That can be easily fixed. The question is why is the comment id missing?

    If you don't have many changes perhaps a fresh install would be the best option.

    Else we could try not to use the get_comment_link() and instead use this :

    add_filter( 'comment_post_redirect', function( $location, $comment ){
    	global $wp_rewrite, $in_comment_loop;
    	$comment = get_comment($comment);
    	$args = array(
    		'type'      => 'all',
    		'page'      => '',
    		'per_page'  => '',
    		'max_depth' => '',
    		'cpage'     => null,
    	$link = get_permalink( $comment->comment_post_ID );
    	// The 'cpage' param takes precedence.
    	if ( ! is_null( $args['cpage'] ) ) {
    		$cpage = $args['cpage'];
    	// No 'cpage' is provided, so we calculate one.
    	} else {
    		if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
    			$args['per_page'] = get_option('comments_per_page');
    		if ( empty( $args['per_page'] ) ) {
    			$args['per_page'] = 0;
    			$args['page'] = 0;
    		$cpage = $args['page'];
    		if ( '' == $cpage ) {
    			if ( ! empty( $in_comment_loop ) ) {
    				$cpage = get_query_var( 'cpage' );
    			} else {
    				// Requires a database hit, so we only do it when we can't figure out from context.
    				$cpage = get_page_of_comment( $comment->comment_ID, $args );
    		 * If the default page displays the oldest comments, the permalinks for comments on the default page
    		 * do not need a 'cpage' query var.
    		if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
    			$cpage = '';
    	if ( $cpage && get_option( 'page_comments' ) ) {
    		if ( $wp_rewrite->using_permalinks() ) {
    			if ( $cpage ) {
    				$link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
    			$link = user_trailingslashit( $link, 'comment' );
    		} elseif ( $cpage ) {
    			$link = add_query_arg( 'cpage', $cpage, $link );
    	if ( $wp_rewrite->using_permalinks() ) {
    		$link = user_trailingslashit( $link, 'comment' );
    	$link = $link . '#comment-' . $comment->comment_ID;
    	return $link;
    }, 99, 2 );

    Otherwise we would need to have a look at the files and add some check-points to see where that id gets removed.

    In such case we would need you to send in admin and ftp creds from our contact form. Please do not share such information in the forum threads as they are publicly visible. So head to our contact form :

    In the subject add
    "Attn: Panos Lyrakis"

    And in the content include admin and ftp info in the following form:

    - Admin login:
    	Admin username
    	Admin password
    	Login url
    - FTP credentials
    	(and port if required)
    - link back to this thread for reference

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.