My First WordPress Plug-In: Customizing the comment notification Emails

,
My First WordPress Plug-In: Customizing the comment notification Emails

SundayGeekeryOne of the things I’ve been super excited about since my move to a self-hosted wordpress.org blog is the ability to change just about anything if I’m willing to put  in the time and figure it out. Sadly, time has been short and I haven’t had the opportunity to try out the zillion ideas floating in my head, but there’s one problem I just couldn’t ignore anymore. I hate the comment notification emails. I’m sure they are perfect for tons of users as they are right out of the box, but I’ve never been fond of them. Time to stretch my techie legs and fix the problem!

Step 1: Figure out exactly what the problem is and the desired behavior.

Problem: I absolutely love some of the conversations generated from this blog through email. I find that depending on commenters to come back to the blog to see if I’ve replied is unfair to them and unlikely to happen. I use the wordpress comment notification emails to reply directly to any commenters that leave their reply email address. WordPress’ default notification emails are geared towards the post author and not commenter friendly.  I would like them to be more commenter focused. Here’s what the email looks like out of the box with some, ahem, annotations.

WordpressDefaultEmail

  • Email Subject line:  Use something directed at the commenter such as “Reply to your comment at TBZ”
  • Intro Line: I’d like the email to include a nice Thank You for the commenter’s time.
  • Commenter Info: Keep commenter’s name, email, and website. Drop all the extra IP/Whois noise
  • Link Backs: Link back to the post the comment was made and label the link as such.
  • Admin Panel Noise: Commenters don’t care where my admin options are! Let’s dump it!

Could I go crazy and make it a flashy email with pictures, css or other web tricks? Heck yeah. If I go down the road of something more sophisticated, I must remember that email clients are different. We all aren’t gmail users. I will want to make sure I have plain text fall back and realize some email clients may not display my email as intended. Virtually all email readers can handle plain text and I’m ok with that (for now mwwhahahahaha) so I’m keeping it simple.

Here’s what the email looks like now that I made some changes:

WordpressEditedEmail

Much better right? Nice and clean for both my and the commenter’s benefit. I’m much happier!

So – why the heck does it require a plug-in?

OK my non-code friends – you’re going to get scared. I’m getting ready to talk code. If you’re not familiar with php functions – this will make your head spin a bit. If you want to learn, I’m always here to help. May I suggest this wiki page for a simple .php function example?

matrixIf I want ALL of my comment notification emails to look the same, there are two php functions I’m going to need to change – wp_nofity_moderator and wp_notify_postauthor.

SIDE QUEST: See you thought you were crazy – you’re moderated emails reply back to wordpress, but you’re pre-approved commenters reply back to the commenter’s address. Now you know why – they are handled as completely separate wordpress functions. I haven’t worked out why that is yet so I didn’t change the behavior of the moderated mail. When I do understand the reasoning, I’ll switch moderated over to behave like pre-approved. WHOA. Awesomeness!

Back to regularly scheduled programming…. It just has happens that wp_notify_moderator and wp_notify_postauthor are both core wordpress pluggable functions. Wait… huh? OK ok, you didn’t click the link – here’s the important message from the pluggable functions page:

“Pluggable functions were introduced in WordPress 1.5.1 These functions let you override certain core functions via plugins. The most up-to-date list of core functions that WordPress allows plugins to override is available at wp-includes/pluggable.php. WordPress loads the built-in functions only if they are undefined after all plugins have been loaded.”

BING! Must override the core functions via a plugin. It is just how it is designed. I suppose there is nothing stopping me from editing pluggable.php – other than it would be a pretty bonehead move. Besides the risk of editing core files and potentially killing my whole site, it would likely be overwritten at my next wordpress upgrade.

Turns out – A simple Plug-in is easy!

You’re more than welcome to download mine and take it apart to see what I changed. You can compare the two functions in this plug-in file to the same functions in the pluggable.php file on your wordpress installation.

If you want – you can try it as it is**. Upload the file* to wp-contentpluginBZcustomEmail (you must name the folder BZcustomEmail and leave the file name BZcustomEmail.php). Go to plugins on your wordpress control panel and activate it. Comment away!

*NOTE: The downloadable version of this file is a .txt with executable options stripped. If you want to use it, you’ll need it to be executable as a .php. Change the file extension from .txt to .php. At the beginning of the file, you will need to add  <?php and at the end of the file add ?>

**NOTE2: This is working fine on my own site, I make no promises on yours. You may have a plugin that conflicts or a different version of the functions. If it doesn’t work, just delete the BZcustomEmail.php file and everything goes back to normal!

OK, it is only easy if you know what you’re doing.

wallHonestly – I get that I just threw a bunch of high level tech concepts out there without explaining a nary one of them.  I tried to put enough out there that you can run with it and make something of it if you’re already fairly comfie mucking around on your server. If you’re not, I’ve included bunches of info that will make great google keyword searches and put you on the right path. Tinker with it, when you hit a wall, let me know. I’ll answer what I can and if you hit a second wall, we’ll bust through that one too!

One last tip, because I can’t help myself!

I like to use Filezilla as an FTP client to move my files from my desktop to my server. It is a nice light and intuitive GUI interface and it is free! Check it out!

Questions? Come on – I know there’s at least one!