1

I am trying to call a function after a comment is saved to the database, see below...

add_action('comment_post', 'myFunction', 10, 2); 
do_action('comment_post', $comment_ID, $comment_approved); 

I have placed this code in my comments.php file. myFunction is defined in functions.php and is shown below

function myFunction($comment_ID, $comment_approved){
    Analytics::track('Posted comment!');
}

The problem is that this function is getting called every time a page containing the comment form is loaded not just when a comment is posted...

Any thoughts on why this is happening???

I guess a work around could be to call the function on page load only when the page is being loaded from wp-comments-post.php but I don't know where to even start trying to achieve this...

Any help is appreciated!

1 Answer 1

1

Your action is called whenever comments.php loads because you're explicitly running it with do_action. Both the function and add_action should be in functions.php, and do_action should be removed entirely.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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