Tim Yang’s Weblog

1/8/2023

Howto: Tell-a-friend script for Wordpress

Filed under: Howto, Wordpress — Tim Yang @ 1:52 pm

I had a tell-a-friend script on my old blog. But Wordpress doesn’t have anything like that, not even in plugin form. So I wrote a quick one. This script is extremely simple. It works but there are no checks, validations or contingencies built into it. So don’t kill me, I’m still on page 28 of PHP for Dummies (”Concantenating is not a Mexican dance”). Caveat emptor, you have been warned.

The script is meant for use with single post pages. It does not have to be in the Loop, so it can be placed in the sidebar of single.php. If you put it on index.php (or any other page), it will simply send the url and page title of the first post you ever made (not good). If I was informing Madame X of this post, the message the script sends out is formated like this (but you can change it where appropriate to suit your taste):

Subject: Have a look at this blog page, Madame X

Message: Hi, Madame X, I found this interesting post on Tim Yang’s Geek Blog called “Howto: Tell-a-friend script for Wordpress” that I thought you would also find interesting. It’s at http://timyang.com/2005/07/howto-tell-a-friend-script-for-wordpress.

Signed, Tim Yang

Here are the instructions.

Paste the following part wherever you want the Tell-a-Friend form to be shown on your pages. With CSS, you can style the inputs, labels and send button with the classes: tafinput, taflabel and tafsubmit.

<!-- Form tellafriend -->
<form id=”tellafriend” method=”post” enctype=”multipart/form-data” action=”<?php bloginfo(’template_directory’); ?>/tellafriend.php”>
<fieldset>
<input type=”hidden” name=”blogname” value=”<?php echo get_bloginfo(’name’); ?>” />
<input type=”hidden” name=”posturl” value=”< ?php echo get_permalink(); ?>” />
<input type=”hidden” name=”posttitle” value=”<?php single_post_title(); ?>” />
<label for=”sendername” class=”taflabel”>Your Name</label>
<input type=”text” name=”sendername” class=”tafinput” />
<label for=”recipientname” class=”taflabel”>Your Friend’s Name</label>
<input type=”text” name=”recipientname” class=”tafinput” />
<label for=”senderemail” class=”taflabel”>Your Email</label>
<input type=”text” name=”senderemail” class=”tafinput” />
<label for=”recipientemail” class=”taflabel”>Your Friend’s Email</label>
<input type=”text” name=”recipientemail” class=”tafinput” />
<input type=”submit” name=”submit” value=”Send it” class=”tafsubmit” />
</fieldset>
</form>
<!- End tellafriend ->

Then create a file called tellafriend.php containing the following code and upload it to your theme folder.

<?php
$mailTo = $recipientemail;
$mailSubject = “Have a look at this blog post, ” . $recipientname;
$redirectPage = $posturl; /* You should change $posturl to something like “sent.php” (with quotes) to redirect to a page that has a confirmation message like ‘Email has been sent!’ */
$mailBody = “Hi, ” . $recipientname . “, I found this interesting post on ” . $blogname . ” called “” . $posttitle . “” that I thought you would also find interesting. It’s at ” . $posturl . “.\n\nSigned, ” . $sendername;
mail(”$mailTo”, “$mailSubject”, $mailBody, “From: ” . $senderemail . “\nReply-To: ” . $senderemail . “\n”);
header(”Location: $redirectPage”);
?>

Voila! Very simplistic.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress