How to Add "Send Article to a Friend" Button in WordPress

Advertisement

Few months ago I’ve posted a complete way to Send Article to your friend using jQuery, yeah it’s quite long and recommended for advance user only. Today I will give you another quick way to send your post by email, this also help your site get high organic traffic by sending great and valuable tutorial to anyone’s friend.

As usual we’re going to implement the functionality using WordPress Shortcode, if you didn’t know shortcode yet please visit WordPress Codex to know more about.

PHP

Copy and paste this snippet into your functions.php of your current theme, if you don’t have functions.php yet, please create a new one.


add_shortcode('send_to_friend', 'email_to_friend');
function email_to_friend($atts)
{
	global $post;
	extract(shortcode_atts(array(
		'anchor_text' => 'Send to Friend',
	), $atts));

	$post_title = htmlspecialchars( $post->post_title );
	$email_subject = 'Sur '.htmlspecialchars(get_bloginfo('name')).' : '.$post_title;
	$email_body = 'I recommend this page : '.$title.'. You can read it on : '.get_permalink($post->ID);
	$send_button = '<a class="send-button" rel="nofollow" href="mailto:?subject='.rawurlencode($email_subject).'&amp;body='.rawurlencode($email_body).'" title="'.$anchor_text.' : '.$title.'">'.$text.'</a>';

	return $send_button;
}

How to use the shortcode?

Put this shortcode into your posts or pages


[send_to_friend]

Template Tag

If ever you want to use the button in your theme .php file, use the code below.


<?php echo do_shortcode('[send_to_friend]'); ?>

If you wish to change the anchor text by using the shortcode, just edit the above shortcode to this


[send_to_friend anchor_text="Send by Email"]

That’s it; hope you find this article useful

To view my old post that use jQuery, Click here Email this article to a friend.

Advertisement