How to perform custom action when WordPress comment is posted?

I am trying to set a custom cookie when a visitor submits a comment, but I can not seem to get this working. Here’s what I have in functions.php:

add_action('comment_post', 'ipro_set_comment_cookie');

function ipro_set_comment_cookie($comment) {
    setcookie("visitor_name", $comment->comment_author, time()+86400);
    setcookie("visitor_email", $comment->comment_author_email, time()+86400);
}

I’ve also tried changing comment_post to wp_insert_comment– neither have seemed to work. I am working off of WP’s action references:
http://codex.wordpress.org/Plugin_API/Action_Reference#Comment.2C_Ping.2C_and_Trackback_Actions

Read More

…any ideas?

Related posts

Leave a Reply

1 comment

  1. Check out the Database writes in the Filter Reference

    Try something like comment_save_pre

    applied to the comment data just prior to updating/editing comment data. Function arguments: comment data array, with indices “comment_post_ID”, “comment_author”, “comment_author_email”, “comment_author_url”, “comment_content”, “comment_type”, and “user_ID”.

    That way it sets on submit (so it calls after your error handling kicks in)

    If I understand your question correctly, this should work:

    add_action('comment_save_pre', 'ipro_set_comment_cookie');
    
    function ipro_set_comment_cookie($comment) {
        setcookie("visitor_name", $comment->comment_author, time()+86400);
        setcookie("visitor_email", $comment->comment_author_email, time()+86400);
    }