WordPress filter hook pre_comment_approved

I was really busy this whole month which makes me neglected Hungred Dot Com a lot. But i will still try to keep up with it as soon as possible. This article will definitely help ANYONE who uses wordpress filter hook ‘pre_comment_approved’. I search Google for a while but couldn’t get any help so i decided to investigate instead. The documentation of this hook is a bit screwed up. I’m sure they will edit it in the future. Nonetheless, for people who are having problem using this (you should be). Here is a solution i can provide.

Problem with pre_comment_approved

The problem i am having with pre_comment_approved is that i am UNABLE to retrieve the comment id that should be attached with this filter hook. Instead, the parameter for this action hook is just a simple status to tell you what status is being applied to a particular comment. The purpose of having this filter hook is to enable anyone to change the status into something else instead of approve eg, ’spam’. But some of us will want to do something else with it and we would like to know which comment has been approved. On the documentation, it said that there is a global variable $comment_ID which you can use in the function that you used to hook pre_comment_approved. Unfortunately, it doesn’t exist. Try using the global variable $comment. You will get null (i have no idea why too). So how do you identify comment are we dealing with?

Solution to find comment ID in pre_comment_approved

After cracking my brain and stress for a while. I stare at the core code of WordPress and found some global variable to test (that is why i know $comment_ID and $comment was empty/null). There is a global variable $comment_id instead of $comment_ID in the core implementation of WordPress. But both of them gives you nothing. Hence, if you like to find the whole detail, your/my best bet will be the global variable $commentdata. Here is a simple way of getting your pre_comment_approved filter work.

add_filter('pre_comment_approved', array($this, 'alertUser'),);
public function alertUser($status){
        global $wpdb, $commentdata;
        echo $commentdata['comment_author'];
        echo $commentdata['comment_author_email'];
        echo $commentdata['comment_content'];
        echo $commentdata['comment_post_ID'];
        // etc..
}

With this, you should save some time figuring how and why the heck it is not working ($comment_ID, this is misleading).

 

Like this post? Share it!

digg 48 Wordpress filter hook pre comment approved reddit 48 Wordpress filter hook pre comment approved stumbleupon 48 Wordpress filter hook pre comment approved delicious 48 Wordpress filter hook pre comment approved furl 48 Wordpress filter hook pre comment approved technorati 48 Wordpress filter hook pre comment approved google 48 Wordpress filter hook pre comment approved myspace 48 Wordpress filter hook pre comment approved facebook 48 Wordpress filter hook pre comment approved twitter 48 Wordpress filter hook pre comment approved
share save 171 16 Wordpress filter hook pre comment approved

No related posts.

About Clay

I am Clay who is the main writer for this website. I own a small web hosting company in Malaysia and i'm available to be hired as individual contractor on elance. You can find me on twitter.
This entry was posted in How-to, Others and tagged . Bookmark the permalink.

Comments are closed.