WordPress webhook plugin sample: Comment Notification via email

5
WordPress Plugin: Comment Notification via email WordPress is a great blog platform. Many people read blogs and leave comments. However, as a blogger, he or she may not stay on the blog 24 hours a day, 7 days a week. Fortunately, WordPress supports a tool called Webhooks that can do “push notification”. To put it in plain words, it is a notification that is triggered by some action happened on your blog, such as someone leaves a comment on your blog. “Webhooks” is a very useful tool, but, there is no simple tutorial to teach me how to do it. Therefore, I put this simple tutorial and a sample script together. Hopefully, this can be useful for everyone. This plugin can send email to notify you whenever someone leaves you a comment. If you have a smart phone that can receive email, you can be notified immediately. All you need is a web site that supports PHP 5. If you do not have a web site, find a free web host instead.

description

WordPress is a great blog platform. Many people read blogs and leave comments. However, as a blogger, he or she may not stay on the blog 24 hours a day, 7 days a week. This webhook & plugin can send you a notification that is triggered by some action happened on your blog, such as someone leaves a comment on your blog. This is also a simple tutorial that can help people who would like to develop more advanced feature by themself.

Transcript of WordPress webhook plugin sample: Comment Notification via email

Page 1: WordPress webhook plugin sample: Comment Notification via email

WordPress Plugin:

Comment Notification

via email

WordPress is a great blog platform. Many people read blogs and leave comments.

However, as a blogger, he or she may not stay on the blog 24 hours a day, 7 days a

week.

Fortunately, WordPress supports a tool called Webhooks that can do “push

notification”. To put it in plain words, it is a notification that is triggered by some

action happened on your blog, such as someone leaves a comment on your blog.

“Webhooks” is a very useful tool, but, there is no simple tutorial to teach me how to

do it. Therefore, I put this simple tutorial and a sample script together. Hopefully, this

can be useful for everyone.

This plugin can send email to notify you whenever someone leaves you a comment.

If you have a smart phone that can receive email, you can be notified immediately.

All you need is a web site that supports PHP 5. If you do not have a web site, find a

free web host instead.

Page 2: WordPress webhook plugin sample: Comment Notification via email

1. Get the plugin from http://www.bp-toys.com/share/demo.zip and unzip the file.

You will find a file called “demo.php”

2. Get a web site that support PHP 5.

3. Use text editor to open the demo.php file. Modify email address (the red text in

the code) in the demo.php. Please read the note below.

4. Upload the revised demo.php to your web site.

5. Add web hook in WordPress admin: go to “Settings” -> “Webhooks”.

Action: choose “comment_post”.

Field: choose any field you want. If you do not know which one you want,

simple choose all. You can press “Ctrl” key then use mouse to choose different

field.

URL: set full path to your demo.php. It looks like: http://www.your-web-

site.com/.../demo.php

Note:

1. Make sure the notification comes from WordPress.com. This can prevent

spammer from programming calling this script and trigger tons of junk emails in

your inbox. If you have WordPress install on your own web, change this name.

2. Change “sendmail_from”. This email should be the one from host email address

3. Change “sendmail_to”. This email should be the one that you will check

frequently.

Page 3: WordPress webhook plugin sample: Comment Notification via email

/*

* Copyright (C) 2013 BP Toys Co. LTD developed by Yuehfu Shih (Lasykids)

* email: [email protected]

*

* Licensed under the Apache License, Version 2.0 (the "License"; you may not

* use this file except in compliance with the License. You may obtain a copy of

* the License at

*

* www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

* License for the specific language governing permissions and limitations under

* the License.

*/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head><title>WordPress Comment Notification Sender</title></head>

<body>

<?php

$mystring = urldecode($_SERVER['HTTP_REFERER']);

$findme = "wordpress.com"; // note 1

$pos = strpos($mystring, $findme);

if ($pos === false)

{

echo "Thank you!"; // simply return a dummy text without doing any action;

}

Page 4: WordPress webhook plugin sample: Comment Notification via email

else {

// Retrieve the request's body and parse it as JSON

$body = @file_get_contents('php://input');

//get notification

// send email

ini_set("sendmail_from", "your_web_host_email@your_web_host.com"); //

note 2

$sendmail_to = "[email protected]"; // note 3

$subject = "WordPress Notification ";

$headers="From: <".$sendmail_to.">\n";

$headers .= "MIME-Version: 1.0\r\n";

$headers .= "Content-type: text/html; charset=UTF-8\r\n";

$headers .= "X-Sender: <".$sendmail_to.">\n";

$headers .= "X-Mailer: PHP\n"; // mailer

parse_str($body, $output);

$message = "WordPress Comment notification!";

$message .= "<br> comment_ID: ".urldecode($output['comment_ID'])." <br>";

$message .= "<br> comment_author:

".urldecode($output['comment_author'])." <br>";

$message .= "<br> comment_author_email:

".urldecode($output['comment_author_email'])." <br>";

$message .= "<br> comment_author_url:

".urldecode($output['comment_author_url'])." <br>";

$message .= "<br> comment_content:

".urldecode($output['comment_content'])." <br>";

$message .= "<br> comment_post_ID:

".urldecode($output['comment_post_ID'])." <br>";

$message .= "<br> comment_author_IP:

".urldecode($output['comment_author_IP'])." <br>";

$message .= "<br> comment_date: ".urldecode($output['comment_date'])."

<br>";

$message .= "<br> comment_date_gmt:

Page 5: WordPress webhook plugin sample: Comment Notification via email

".urldecode($output['comment_date_gmt'])." <br>";

$yesNo = (urldecode($output['comment_approved']) == 1) ? "yes" : "no";

$message .= "<br> comment_approved: ".$yesNo." <br>";

$message .= "<br> comment_agent: ".urldecode($output['comment_agent'])."

<br>";

$message .= "<br> comment_type: ".urldecode($output['comment_type'])."

<br>";

$yesNo = (urldecode($output['comment_parent']) == 1) ? "yes" : "no";

$message .= "<br> comment_parent: ".$yesNo." <br>";

$message .= "<br> user_id: ".urldecode($output['user_id'])." <br>";

$yesNo = (urldecode($output['approval']) == 1) ? "yes" : "no";

$message .= "<br> approval: ".$yesNo." <br>";

$message .= "<br> hook: ".urldecode($output['hook'])." <br>";

mail ($sendmail_to,$subject,$message,$headers,$nav);

} // end of check source: WordPress

?>

</body>

</html>