0

This is driving me crazy - Does anyone know why this 'comment_post' hook doesn't seem to work!? I've basically written an entire email notification plugin for it and it's failed on the first step.

After research I was sure this was the right hook as it is after the comment is saved to the database.

add_action( 'comment_post', 'test_notify' );

function test_notify($args , $args2) {
    //test
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $email = 'address@mail.com';
    $message = 'Args=' . $args . '<br>Args2=' . $args2;
    wp_mail( $email , 'test', $message, $headers);

    echo 'It works!';
    header("Location: /blog?thisworked=true");
}

When a comment is posted, the function is not triggered... at all? The do_action is on line 2095 of /wp-includes/comment.php

do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
1

Does anyone know why this 'comment_post' hook doesn't seem to work!?

...

When a comment is posted, the function is not triggered... at all?

The hook works. Try the following, it works:

add_action( 'comment_post', 'test_notify' );
function test_notify($args , $args2) {
    echo 'It works!'; 
    die;
}

As does this:

add_action( 'comment_post', 'test_notify' );
function test_notify($args , $args2) {
    echo 'It works!'; 
    header("Location: http://www.google.com");
    die;
}

This, in fact, works also but you are likely to get a 404:

add_action( 'comment_post', 'test_notify' );
function test_notify($args , $args2) {
    echo 'It works!'; 
    header("Location: /blog?thisworked=true");
    die;
}

You want to be using site_url() or home_url() probably:

add_action( 'comment_post', 'test_notify' );
function test_notify($args , $args2) {
    echo 'It works!'; 
    header('Location: '.home_url('/blog?thisworked=true'));
    die;
}

If those don't work, something is interfering and I don't know how to guess what.

  • Thanks for your reply - you are right - the hook does work. Putting the echo and die in echo all the variables back fine. It's only when I seem to add anything to do with wp_mail in this particular function that it stops working - is there any particular conflict between wp_mail and comments? – Bysander Jul 22 '15 at 16:29
  • What, if anything, still does not work? – s_ha_dum Jul 22 '15 at 16:30
  • Sorry - accidental enter - have updated comment! – Bysander Jul 22 '15 at 16:30
  • if I use the basic mail(... function it works fine... But ignores my SMTP routing and filters to add company branding etc as not attached to the wp_mail hooks – Bysander Jul 22 '15 at 16:35
  • Ah... where/how are your "SMTP routing and filters" added? – s_ha_dum Jul 22 '15 at 16:50

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