Question

I've been trying to direct the user to the specific page after they posted the comment, but that not luck for me.

Here is the my code blocks

My first try:

add_action('wp_insert_comment', array($this, 'redirectAfterCommented'));

public function redirectAfterCommented() {

   //some bussines logics

   wp_redirect('http://example.com/sample-jpage');

}

Also, I've try with another with the following code

add_action('comment_post', array($this, 'redirectAfterCommented '), 10, 2);

public function redirectAfterCommented () {

   // some business logic

   wp_redirect('https://example.com/sample-page');

}

Do hooked in the wrong place? if so, where should I hook to redirect the commentator?

Was it helpful?

Solution

Use the filter comment_post_redirect:

add_filter( 'comment_post_redirect', 'comment_redirect' );

function comment_redirect( $location ) {
    $location = 'http://example.com/sample-page';
    // or:
    // $location = get_page_link( page_ID );
    // $location = get_page_link( get_page_by_path( 'sample-page' )->ID )
    return $location;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top