Approve comment hook?

I’m looking to send out emails to subscribers when a comment has been approved.

The two actions in the Codex are:

1.

add_action('comment_post', 'callback', $priority, $accepted_args);

Where the arguments are comment_ID and approval status (0 or 1).

2.

add_action('edit_comment', 'callback', $priority, $accepted_args);

With argument comment_ID

By default comments are not approved when they are posted so I think I would be editing them when I approve them but it’s unclear in the Codex. Which option should I use when I approve a comment?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just like posts, a comment can have an array of different statuses, so instead of naming a hook with each status, they have transition hooks, which tell you what status it had before and what’s the new status. In your case, this might do the trick:

add_action('transition_comment_status', 'my_approve_comment_callback', 10, 3);
function my_approve_comment_callback($new_status, $old_status, $comment) {
    if($old_status != $new_status) {
        if($new_status == 'approved') {
            // Your code here
        }
    }
}

Let us know how it goes?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x