WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking to call a new javascript function only after a new comment has been made by a user. Specifically, I want to include new javascript on the resulting page of the page reload after submitting the new comment form on a blog post.

Does anyone know how to do this?

My end goal is that I want to make a call using Mixpanel's (http://www.mixpanel.com) javascript API call and make an event called "Comment added" for a specific user. I know how to do the mixpanel side of things but am confused by the this specific use case of wordpress.

Thanks in advance!

share|improve this question
up vote 1 down vote accepted

I am not so clear where exactly you would want to "inject" this JS . "After a new comment has been made" , in WP terms and php workflow, can mean several things . (for example when the $POST request is made , when the action is triggered ,when it is getting checked, sanitized , when it is inserted to the DB etc etc ..)

Anyhow ,you will need to search for the right wp actions / filters that you can hook into , like the comment_post action ( unfortunately undocumented in codex - GIYBF ) ,or the comment_post_redirect filter (Also undocumented in codex) or wp_insert_comment action (runs AFTER comment inserted to DB) and use those actions to include the JS ..

for example :

add_action ('comment_post','my_comments_js_function');// runs when comments are POSTED

and then

// do your JS stuff.

But like I said, it is not really clear to me what exactly you want and WHEN .

share|improve this answer
Thanks Krembo, This should help me get started! As far as 'when', I believe the best time would after the comment has been added to DB and when the page is reloaded to show the new comment. Either way, I appreciate the help! – Danny Dover Mar 7 '13 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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