After someone submits a comment this line of codes redirects him to the post (i believe). Can I modify the code in order to redirect him to a custom URL?

do_action('comment_form', $post->ID)

Ty for the answer,


my comments.php

  <input name="submit" type="submit" id="submit" tabindex="5" value="Submit" />
  <input type="hidden" name="my_redirect_to" value="http://www.google.com"; />
  <?php comment_id_fields(); ?>
  <?php do_action('comment_form', $post->ID); ?>

my functions.php

add_action('comment_post_redirect', 'redirect_to_thank_page'); // Redirect to thank you post after comment
   function redirect_to_thank_page() {
       if ( isset( $_POST['my_redirect_to'] ) ) 
       return $_POST['my_redirect_to']; 

The code does not work, it's not redirecting to google.com for example.

Any ideas why? Ty


Not quite; the redirect occurs inline inside wp-comments-post.php

Use the filter comment_post_redirect to pass back any URL of your choice. Arguments passed are the default redirect & comment object, respectively.

Based on your comments, here's a suggestion:

function wpse_58613_comment_redirect( $location ) {
    if ( isset( $_POST['my_redirect_to'] ) ) // Don't use "redirect_to", internal WP var
        $location = $_POST['my_redirect_to'];

    return $location;

add_filter( 'comment_post_redirect', 'wpse_58613_comment_redirect' );
  • Ty very much, I can't use a filter though.... is there a method to modify the form directly inside comments.php? – webmasters Jul 16 '12 at 1:22
  • Are you saying you want to change where the form is submitted to? Or what happens once a comment is posted? – TheDeadMedic Jul 16 '12 at 9:22
  • i cannot use filters because I am using a custom code which passes a variable mysitec.om/post/customvar - and maybe I can use something like <input name="redirect_to" type="hidden" value="domain.com/post/customvar" /> in comments.php – webmasters Jul 16 '12 at 11:20
  • 1
    Sorry, but you're not making an awful lot of sense - I've taken a guess at what you're after (see the revision). – TheDeadMedic Jul 16 '12 at 11:45
  • I think it work, ty very much:) Had to replace google.com with my actual url – webmasters Jul 16 '12 at 16:11

protected by Community Jan 29 '13 at 15:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.