Hero Inc.

where heroes are an everyday occurrence

Jul-26-2008

No More WP Comment Emails

For those who have been around my site for sometime, you’ve known about my previous work creating my own CMS (Content Management System). I tried WordPress about 3 years ago and did not like it initially due to my limited knowledge of PHP and ability to change/modify functionality that it offered. However, I soon learned how much time I needed to truly build the CMS that would accomplish all of the tasks that I needed and I soon moved back to a happier WordPress time.

Well, after several months of WordPress usage, one minor detail has continued to grow on my nerves. WordPress can be setup to notify the author of the post of any comments that are made concerning the post. I had built this functionality into my own CMS but I stopped comments from being emailed if I made them. I mean, why would I want to read my own post comments? I only want to read those that others have left.

Since I had built this functionality before I knew it was possible. I just needed to take the time to find where I needed to make the change within WordPress. One night earlier this week, I finally took the time to find the file I needed to change and so far my change has worked. For those interested, here is what you need to do.

  1. Find the “comment.php” file in your WordPress files. It is listed under the “wp-includes” folder.
  2. Inside of this file you will need to find the “wp_new_comment” function. This is the function that inserts comments into the WordPress database. It also utilizes the PHP mail function to send mails to the author’s email address to notify of any new comments.
  3. Within the function, you will find the following IF statement that needs to be altered:
    if(get_option('comments_notify') && $commentdata['comment_approved'] &&
      $post->post_author != $commentdata['user_ID'])
         wp_notify_postauthor($comment_ID, $commentdata['comment_type']);

    This statement is saying to notify the post author if certain conditions are met. This function is great but we need to alter it so that for a particular case we do not notify the author.

  4. I modified my statement to be based on the email address since that is unique to an individual. Here is the changeI needed to make:
    if(get_option('comments_notify') && $commentdata['comment_approved'] &&
      $post->post_author != $commentdata['user_ID'] &&
      $commentdata['comment_author_email'] != 'username@myemail.com')
         wp_notify_postauthor($comment_ID, $commentdata['comment_type']);

    I basically added the extra condition that if everything else is right for emailing the author about the post that the system should make sure the commenter’s email address does not equal a certain value. For those that might try it on my site, the email address above is not the one I used on the code change for Hero Inc. I just wanted to provide an example here.

That’s it. That’s all that needed to change for the emails to stop when I posted comments on my own posts. When I made this coding change, I also modified a few other sections of my site that were bugging me as well. Namely the comments section was changed from the design perspective. I have hated for a long time how the comments were displayed using an ordered list of numbers.

Let me know if you do or do not like the latest comment design changes. Man, it was nice to code for “fun” again.

Posted under Internet | Tagged as

Comments

Will Says:
July 27th, 2008 at 9:45 am

“Code for fun”

Is not that a oxymoron?

Ben Says:
July 27th, 2008 at 11:13 am

What can I say?

I’m a nerd and I enjoy what I like to do. I even spent time last night making some other changes as well.

Kelvin48 Says:
October 22nd, 2009 at 5:55 pm

OH my gosh, great questions! ,

Add A Comment

If you would like to track when new comments have been made, subscribe to the Comments Feed.