apply_filters( 'comment_post_redirect', string $location, WP_Comment $comment )

Filters the location URI to send the commenter after posting.


Description Description


Parameters Parameters

$location

(string) The 'redirect_to' URI sent via $_POST.

$comment

(WP_Comment) Comment object.


Top ↑

Source Source

File: wp-comments-post.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
2.0.5 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Aurovrata Venet

    Sometimes you want to build a page with various custom posts displayed on the same page and use one of these post’s comments to handle the page comments. However, when you submit a comment associate with a given post, the submit button redirects you to that post’s permalink, and not to your original page. To overcome this, you need to filter the redirect location such as,

    add_filter( 'comment_post_redirect', 'redirect_comments', 10,2 );
    function redirect_comments( $location, $commentdata ) {
      if(!isset($commentdata) || empty($commentdata->comment_post_ID) ){
        return $location;
      }
      $post_id = $commentdata->comment_post_ID;
      if('my-custom-post' == get_post_type($post_id)){
        return wp_get_referer()."#comment-".$commentdata->comment_ID;
      }
      return $location;
    }

    Don’t forget to also change the ‘Reply’ button links on comments using the filter comment_reply_link

You must log in before being able to contribute a note or feedback.