scriptygoddess

05 May, 2002

How to set up a "subscribe to comments" feature on your blog (MT specific)

Posted by: Jennifer In: How to's

Leaving this post here for archival purposes – but the latest version of the script is now in cgi format – find more info here

This is so easy you're going to laugh… it's very much cut and paste, very little customzing.

Please note that very soon I'll be re-doing things here so that scripts like this will be in a download section, and you'll just be able to download the whole thing with a readme of instructions… but in the meantime….

Also note – this was a team effort between myself and Lynda (who you can see from the last post did quite a job on this!)

Step 1
(this will be the "hardest" most customizing of this script)
part a:
on your individual entry page – go to the comments form. In the form tag for the comments change this line:

action="<$MTCGIPath$>mt-comments.cgi"

to:

action="http://www.YOURSITE.COM/PATH_TO_THIS_FILE_YOU_WILL_BE_CREATING_VERY_SOON/emailtolist.php"

part b:
you'll see a few hidden tags… add this one:

<input type="hidden" name="title" value="<$MTEntryTitle encode_html="1"$>"/>

part c:
This will allow people to automatically subscribe/unsubscribe to comments when they enter a new comment.
add this somewhere BEFORE end form tag (</form>):

<input type="radio" name="subscription" value="subscribe"> Subscribe<br>
<input type="radio" name="subscription" value="unsubscribe"> Unsubscribe<br> (email field must be filled in)

part d:
This will allow people to subscribe/unsubscribe to comments WITHOUT having to enter a new comment.
Add this outside of the comment form:

<form action="http://www.YOURSITE.COM/PATH_TO_THIS_FILE_YOU_WILL_BE_CREATING_VERY_SOON/addsubscriber.php" method="post" name="addsubscriber">
Enter email address to subscribe/unsubscribe to comments on this post without having to post a comment:<br />
<input type="radio" name="subscription" value="subscribe"> Subscribe<br>
<input type="radio" name="subscription" value="unsubscribe"> Unsubscribe<br> (email field must be filled in)
<input type="hidden" name="entry_id" value="<$MTEntryID$>" />
Email: <input name="email" size="40" />
<input type="submit" value="submit" name="submit">
</form>

Step 2
Create a folder called "commentsubscribe". CHMOD this folder to full read/write/execute access (777)

Step 3
Copy and paste the following into a file. Name it: emailtolist.php
(Make sure to change the appropriate variables -marked in red – to match your setup)

<?
$text = stripslashes($text);
$fileString = "/home/PATH_TO_YOUR_FOLDER/commentsubscribe/$entry_id.txt";
if ($subscription == "subscribe")
{
if (file_exists($fileString)) {
$emailList = file("$fileString");
for ($i = 0; $i < count($emailList); $i++) {
if (trim($emailList[$i]) == trim($email)) {
$doRun = false;
}
}
}
if (!isset($doRun)) {
$file = fopen("$fileString", "a+");
if ($file) {
fputs($file, $email);
fputs($file, "\n");
fclose($file);
}
}
}
if (file_exists($fileString))
{
if ($subscription == "unsubscribe")
{
$array = file($fileString);
$file = fopen($fileString, "w");
if ($array)
{
foreach($array as $oldemail)
{
$oldemail = trim($oldemail);
if ($oldemail != $email) fputs($file, "$oldemail\n");
}
fclose ($file);
}
}
}
if (!isset($isSent)) {
if (file_exists($fileString)) {
$emailList = file("$fileString");
$count = count($emailList);
for ($i = 0; $i < $count; $i++) {
$recepient = trim($emailList[$i]);
$title = stripslashes($title);
mail("$recepient ", "New Comment on $title", "Author: $author\ncomment: $text", "From: YOU@YOURDOMAIN.COM");
}
}
$submitForm = "document.comments_form.submit()";
$isSent = true;
} else {
$submitForm = "document.location='http://YOUR_HOME_PAGE.COM'";
/*
I put this here in case someone tried to reload THIS page…
that way the $isSent variable is set to true (at the bottom)…
and the form won't process again post the comment again
and annoy people with bogus emails
*/
}
$text = htmlspecialchars($text);
?>
<html><head><title></title></head><body onLoad="<? echo $submitForm ?>">
<form method="post" action="http://LOCATION_OF_YOUR_MT_INSTALLATION/mt/mt-comments.cgi" name="comments_form">
<input type="hidden" name="static" value="<? echo $static ?>" />
<input type="hidden" name="entry_id" value="<? echo $entry_id ?>" />
<input type="hidden" name="ipadd" value="<? echo $ip ?>" />
<input type="hidden" name="author" value="<? echo $author ?>" />
<input type="hidden" name="email" value="<? echo $email ?>" />
<input type="hidden" name="url" value="<? echo $url ?>" />
<input type="hidden" name="text" value="<? echo $text ?>" />
<input type="hidden" name="post" value="submit" />
</form>
<?
$isSent = "true";
?>
please wait…
your comment is being submitted.
</body>

Step 4
Copy and paste the following into a file. Name it: addsubscriber.php
(Make sure to change the appropriate variables -marked in red – to match your setup)

<?
$fileString = "/home/PATH_TO_YOUR_FOLDER/commentsubscribe/$entry_id.txt";
if ($subscription == "subscribe")
{
if (file_exists($fileString)) {
$emailList = file("$fileString");
for ($i = 0; $i < count($emailList); $i++) {
if (trim($emailList[$i]) == trim($email)) {
$doRun = false;
}
}
}
if (!isset($doRun)) {
$file = fopen("$fileString", "a+");
if ($file) {
fputs($file, $email);
fputs($file, "\n");
fclose($file);
}
}
}
if (file_exists($fileString))
{
if ($subscription == "unsubscribe")
{
$array = file($fileString);
$file = fopen($fileString, "w");
if ($array)
{
foreach($array as $oldemail)
{
$oldemail = trim($oldemail);
if ($oldemail != $email) fputs($file, "$oldemail\n");
}
fclose ($file);
}
}
}
header("Location: http://YOUR_HOME_PAGE.COM/");
exit;
?>

features to be added soon (but not yet on here)… the ability to GLOBAL REMOVE oneself from ALL subscriptions…. Also – this is not setup for "preview comment"…coming soon…

32 Responses to "How to set up a "subscribe to comments" feature on your blog (MT specific)"

1 | Christine

May 5th, 2002 at 1:08 am

Avatar

Jenn? Lynda? I love you. I love you. I truly, truly love you. YOU ROCK. You rock my world. You have made all of my dreams come true. THANK YOU!!! I'll test it out on the blog tomorrow!

2 | Lynda

May 5th, 2002 at 3:21 am

Avatar

Jenn – I already wrote up the unsubscribe unless you were unhappy with that. Tested that out and it worked just fine. Curious why you left it out of the tutorial.

🙂

3 | Jennifer

May 5th, 2002 at 7:36 am

Avatar

I left it out because I'm a dufus! LOL!!! I was SO tired last night. Friends shouldn't let friends drink and write tutorials. LOL!

I'm updating the post (and the code on the site) now…

4 | Jennifer

May 5th, 2002 at 8:26 am

Avatar

Ok this should be all set… let me know if I left anything else out… If I get a minute to myself today/tonight – I'll try to get this working with PREVIEW COMMENT

5 | Tobias

May 5th, 2002 at 11:57 am

Avatar

Shaweeeeet, thanks for the great hack. I'll try to implement it as soon as I find the time.

6 | Christine

May 5th, 2002 at 1:12 pm

Avatar

Jenn – just an idea as I have the subscription e-mails coming through on this post – could it include the URL link to the post? And maybe one to the main site too? I think it would be helpful as a reminder to what the post was about! LOL! I haven't started to play with this yet on my sites, so when I dig in to it I might be able to figure out on my own where to post the links.

7 | Lynda

May 5th, 2002 at 1:35 pm

Avatar

It can include anything you want it to include. Just pass it along as a hidden field inside your form on the comment template and then put it into the message.

Something like this on the form:

<input type="hidden" name="link" value="

8 | Lynda

May 5th, 2002 at 1:37 pm

Avatar

Blah. Might help if I put the code in correctly. The hidden field (if you didn't catch it) should look like this:

<input type="hidden" name="link" value="<$MTEntryLink$> "/>

9 | Lynda

May 5th, 2002 at 2:15 pm

Avatar

Another note – if the emailtolist.php page is called up on its own, it will still try to submit to mtcomments (as the isSent variable hasn't been set yet). That wouldn't really cause a problem as MT would just generate an error, but it isn't pretty.

So I suggest changing this line:

if (!isset($isSent)) {

To this:

if (!($entry_id == "") && (!isset($isSent))) {

That basically says if the entry_id is blank AND the variable isSent hasn't been sent yet, proceed with the commands.

I tested it out and haven't seen any problems with it.

10 | Jennifer

May 5th, 2002 at 2:27 pm

Avatar

I'll update the script tonight and add in the the link to the post, as well as the ability to use this with the preview comments… so stay tuned…

11 | Lynda

May 5th, 2002 at 2:44 pm

Avatar

If you're reading this in the comments, you might want to check it out on the site for clearer viewing pleasure:

I found a nice way to add an automatic unsubscribe option to each email sent out (so people will have an easier time unsubscribing, which I'm all for)

Change this:

mail("$recepient ", "New Comment on $title", "Author: $author\ncomment: $text", "From: YOU@YOURDOMAIN.COM");

To this:

mail("$recepient ", "New Comment on $title", "Author: $author\ncomment: $text\n\n\nUNSUBSCRIPTION INFO: I understand if you no longer wish to receive comments for this entry. Please click on the below link to be automatically unsubscribed:\nhttp://WWW.YOURSERVER/PATHTO/addsubscriber.php?entry_id=$entry_id&email=$recepient&unsubscription=unsubscribe", "From: YOU@YOURDOMAIN.COM");

In order for it to work though, you need to get rid of this line in the addsubscriber.php code:

header("Location: http://YOUR_HOME_PAGE.COM/");
exit;

I suggest replacing it with something like:

if ($subscription=="unsubscribe") {
?>
<html>
<head>
<title>Sorry to see you go</title>
</head>
<body>
<p>
You have been unsubscribed to entry #<? echo $entry_id ?> using email address <? echo $email ?>
</p>
</body></html>
<?
} else {
header("Location: http://YOURSITE.COM/");
exit;
}

(and then the closing php tag.)

You can spruce up the HTML to make it match your site, whatever.

12 | Lynda

May 5th, 2002 at 3:40 pm

Avatar

Oooookay, one more little convenient improvement.

The way the code is now, when you subscribe/unsubscribe on the separate form, you get sent back to the main site, which I think is a little inconvenient when you were on an entry page.

You can add this hidden field in the *SEPARATE* subscription form:

<input type="hidden" name="redirect" value="<$MTEntryLink$>" />

And then on the addsubscriber.php page, replace this line (assuming you've changed it to my above code):

} else {
header("Location: http://YOURSITE.COM/");
exit;

to THIS:

} elseif (isset($redirect)) {
header("Location: $redirect");
exit;
} else {
header("Location: http://YOURSITE.com");
exit;
}

If you haven't changed it to my above code, just change the header and exit line to this:

if (isset($redirect)) {
header("Location: $redirect");
exit;
} else {
header("Location: http://YOURSITE.com");
exit;
}

Let me know if there are any problems

13 | Tobias

May 5th, 2002 at 5:35 pm

Avatar

Ehm, I like the hack, but I#m getting confused with all the additions. Do you think it would be possible for you to put the two php files and a set of instructions (for installation and the necessary lines in the Individual entry template) into a small zip file and put it up for download. That would be very much appreciated.

14 | Jennifer

May 5th, 2002 at 5:37 pm

Avatar

I made a few modifications (to incorporate the whole preview comment thingy… also I'm trying to make implementation easier by putting all customization into tags that people set in the beginning… that way they don't have to read through the code… I'm trying it out right now… hopefully Rachel will stay napping so I can finish this up! LOL!

But I'm using MOST of what you're posting… just with some slight modifications…

15 | Jennifer

May 5th, 2002 at 5:43 pm

Avatar

Tobias – I will be updating the original tutorial in a few hours with all the feedback mentioned in the comments… Hopefully by tonight it should be done (and it will be **a lot** easier to follow) As for the zip idea… As I said in the post – it's on the to-do list… It's just hard to find the time to do all this. LOL! But this script was too cool not to "rush it to market".

16 | Lynda

May 5th, 2002 at 7:51 pm

Avatar

It might be a good idea for this and future scripts to create a config.php where people set all the variables they'll need in a clearly marked and documented file and then just require that file in the added php files.

That way it will be super easy to post updates to the script, etc, as people will only need to update the most recent file(s) and keep the same config.php.

17 | Jennifer

May 5th, 2002 at 9:47 pm

Avatar

ok, I'm trying to do that now… but I'm running into a small problem… I've seperated the editable stuff into a config page. And the "templates" into a seperate page, but when I include the templates (the html stuff) – it doesn't seem to know the variables that we're putting in there (all those hidden fields, etc…) I tried doing an include and a require… neither seemed to do it… any thoughts?

18 | Lynda

May 5th, 2002 at 10:00 pm

Avatar

I'm not sure how you're doing it. All the fields are what create the variables, etc.

I would recommend creating a config.php asking for the following information:

$path = "/home/PATH_TO_YOUR_FOLDER/commentsubscribe/";
$fromemail = "YOU@YOURDOMAIN.COM";
$yoursite = "http://WWW.YOURSITE.COM";
$mtpath = "http://LOCATION_OF_YOUR_MT_INSTALLATION/mt/mt-comments.cgi";

Require the file in both the addsubscriber.php and emailtolist.php, then replace the following:

$fileString = "$path$entry_id.txt";

mail("$recepient ", "New Comment on $title", "Author: $author\ncomment: $text", "From: $fromemail");

$submitForm = "document.location='$yoursite";

<form method="post" action="<? echo $mtpath ?>" name="comments_form">

header("Location: $yoursite");

======

One file to edit, plug and play. As with most scripts, going into the meat of the script to change things is on an "advanced" level for those who know what they're doing.

19 | Lynda

May 5th, 2002 at 10:14 pm

Avatar

I didn't include my changes in the examples because I'm a dolt, but you see where I'm going.

The config.php should be required in the scripts, not the other way around. Again, I'm not really clear on what you're trying to do.

If you want to make the templates customizable (so it will look pretty and be easy to update) I would suggest including two .tmpl files (maybe emailtolist.tmpl and addsubscriber.tmpl)

emailtolist.tmpl would look like this as default (people could customize it if they want):

please wait…
your comment is being submitted.

And then be included on in the addsubscriber.php like so:

<?
$isSent = "true";
include '$path' . 'emailtolist.tmpl';
?>

Then addsubscriber.tmpl could contain the whole html – html part and be included into addsubscriber.php like so:

if ($subscription=="unsubscribe") {
inclue '$path' . 'emailtolist.tmpl';
}

Might want to include another line in the config file for "templates path" and then edit that $path variable accordingly. Otherwise, they'll need to be instructed to put the template files in the same folder as they setup for the comment subscriptions.

20 | Jennifer

May 6th, 2002 at 1:05 am

Avatar

okay, folks – over here is the DOWNLOAD for the script… yes you read that correctly… Zipped it all up for you so you can simply download the code. And xmas is months away… ;P LOL!
Here's the new post about this

21 | Jennifer

May 6th, 2002 at 1:17 am

Avatar

Those of you on the email list probably got a funny url for the unsubscribe… had to make a slight adjustment to the code…

See.. it's happening already… Take the code. Play with it… there's probably more that needs to be fixed.. just let me know. 🙂

22 | Lynda

May 6th, 2002 at 7:03 am

Avatar

hehe. I'm just posting this to see what happens. We need either a separate option to configure the URL to the script directory or just make sure the templates don't have php extensions, but I'm sure that's been taken care of (I think)

This is what happens when two tired people work on a script. LOL.

23 | Jennifer

May 6th, 2002 at 7:53 am

Avatar

…duh… maybe I'm just too tired… I didn't follow you.

In the config file there's a place where you specify where the files are.

The templates have .tmpl extensions…(as we talked about yesterday)…

Is that what you meant? If so, yeah, it's in there!! (see the new post – with the download link)

24 | Lynda

May 6th, 2002 at 7:56 am

Avatar

Yeah, Jennifer, that's what I meant. When I got comments it was posting the path instead of the url to the unsubscribe and I didn't know if that was fixed. I was just posting to test it out (although I guess I could have looked at the file you uploaded. hehe)

I think we're both way, way too tired.

25 | Jennifer

May 6th, 2002 at 8:05 am

Avatar

you know, Lynda, I just thought of something… with the comment previewing… unfortunately, I'm forcing users to CHOSE AGAIN if they want to subscribe or not – because there was no way to capture that data from the previous page with a echo $subscription (since the preview page is cgi generated…

BUT… Isn't there that new thing in MT where you can create a MT variable and call it later on…? Can this be used here somehow?

Just food for thought… I think the script is fine as it is… after spending all week on it, I think we could use a break from adding new features to it! LOL! I'm sure people will be coming back with more bugs for me to fix on it.

26 | Jennifer

May 6th, 2002 at 8:08 am

Avatar

hehehe… shows you how tired I am… I meant all weekEND. LOL!

(Hey, could someone kick me in the shins today if I start to fall asleep at work??) LOL!

27 | Lynda

May 6th, 2002 at 8:49 am

Avatar

Let me know if I'm wrong, this is how I see preview being handled:

If POST, then:
1) subscribe or unsubscribe
2) send out the email to the list
3) send the form along to mt-comments.cgi

If PREVIEW, then:
• JUST send the form along to mt-comments.cgi

Seems like this negates the point of having the preview at all because if someone previews their post first, the comment will NOT be sent out to the list and you can NOT subscribe or unsubscribe.

Unless you want to do tweaking to the Perl (which I don't recommend as it would HIGHLY confuse people) then I suggest doing the following (I sent this in a suggestion among my swarms last night):

We take out mt-comments.cgi completely from the preview option and preview the comment on the emailtolist.php page.

If PREVIEW:
1) Subscribe or unsubscribe
2) Echo all the variables in a logical format to allow users to preview their comment. Something like "This is how your post will appear:" and then Name: and so on until all fields are displayed. Hopefully you get my drift with this.
3) Also display a form where people can change their post and echo all the variables in the value field of the form.
4) Give the option to preview (again) or post
5) If PREVIEW, just resubmit the data to the emailtolist.php and start over again (with the updated information)
6) If POST,
6-a) Email the comment to the list
6-b) Send the form along to mt-comments.cgi

I can do this later today if you're not getting what I'm talking about. This is good too, because it allows people to customize their preview page, which is something people have been wanting with MT for a while.

28 | Jennifer

May 6th, 2002 at 9:11 am

Avatar

no… not quite right…
If post or preview – in both cases it's being sent to emailtolist.php

if you hit "preview" the comment is not mailed yet. From the preview page – (because you're changing the "action" line on that template as well – see the readme) it's going to emailtolist.php…

We could make our own "preview" page, but that seems more of annoyance (having to set up ANOTHER MT-type template) when just asking the user to click on the "subscribe" again from the preview comments page is simple enough.

Do you follow me?

Again, check out the new files (there's a new post here with the download link… ) read through the read me – try it out… You'll see what I mean.

29 | Lynda

May 6th, 2002 at 9:20 am

Avatar

Okay, is the ONLY problem with this the subscribe/unsubscribe thing? I see what you're talking about now (Lynda? Read DOCUMENTATION? Ha!)

On the emailtolist.php, why can't we just take the subscribe/unsubscribe script out of the if ($post) so it does that BEFORE it looks whether or not we're trying to preview or post. If someone subscribes, then preview, it will subscribe, then find it's previewing and pass the info along to the preview form.

Make any sense? (Please say yes because I'm tired of not making any sense)

🙂

30 | Jennifer

May 6th, 2002 at 9:37 am

Avatar

LOL! Yeah, makes sense but… I won't be able to implement it until tonight 🙂

bascially – emailtolist.php
will now do this – subscribe or unsubscribe the person… THEN send them to mt-comments.cgi (to preview) ie – once they get to that preview page – they're already subscribed or unsubscribed.

Shouldn't be too difficult to change. I'll take care of it once Rachel's asleep tonight.

31 | Amy

May 6th, 2002 at 3:51 pm

Avatar

Wow. This is really neat! I've been wanting something like this for a long time 🙂

32 | Lynda

May 7th, 2002 at 8:10 am

Avatar

If you're following this thread, the most recent (and very easy to install) version of this script can be located here:

http://www.scriptygoddess.com/archives/000982.php

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements