Awarding points for users who “Share” a post.

You can award points for real shares to social media sites in myCred 1.5 using the ShareThis plugin.

Navigation:

  1. Description
  2. Requirements
  3. The plan
  4. The Shortcode
  5. Done

Description

In this tutorial I will show you how to use the mycred_link shortcode to award users points for “sharing” a post on your website on Facebook, Google+, Twitter or Pinterest.

Important!

Using the mycred_link shortcode will award or deduct users points for that click, however it does not guaranty that the user actually “shares” the content. It is essentially just “Points for clicking on links”.

Note
Code snippets presented here goes in your custom plugin or in your themes functions.php file.

Requirements

  • myCred 1.1
  • The “Points for clicking on links” Hook enabled

The plan

For this tutorial, we will create a new shortcode that will work just as the mycred_link shortcode, with one difference. Instead of entering a URL for the href attribute, we will only enter the social media sites name. The shortcode then append the appropriate API url with the current pages address.

This tutorial will not require us to make any changes to myCred or the mycred_link shortcode!

The Shortcode

Lets start constructing our new shortcode. For this tutorial I have named it “mycred_share_this”.

add_shortcode( 'mycred_share_this', 'mycred_render_shortcode_share_this' );
function mycred_render_shortcode_share_this( $attr, $link_title )
{

}

Since our shortcode will do exactly the same thing as the mycred_link shortcode, we can just pass on our variables directly to the function that renders it.

add_shortcode( 'mycred_share_this', 'mycred_render_shortcode_share_this' );
function mycred_render_shortcode_share_this( $attr, $link_title )
{
	return mycred_render_shortcode_link( $attr, $link_title );
}

Facebook Example

At this stage, we have two shortcodes doing the exact same thing, so now we will need to make the url adjustment. Instead of a URL we will simply pass on the social media site names i.e. [mycred_share_this href=”facebook”].

Facebook offers an easy share dialog that only requires a urlencoded address.

add_shortcode( 'mycred_share_this', 'mycred_render_shortcode_share_this' );
function mycred_render_shortcode_share_this( $attr, $link_title )
{
	$url = get_permalink();

	if ( $attr['href'] == 'facebook' )
		$attr['href'] = 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode( $url );

	return mycred_render_shortcode_link( $attr, $link_title );
}

As you can see it is pretty straight forward. We get the current pages url though get_permalink() and append it to the share dialog url. Below I have entered a few different social media sites but you can add others you might find.

add_shortcode( 'mycred_share_this', 'mycred_render_shortcode_share_this' );
function mycred_render_shortcode_share_this( $attr, $link_title )
{
	$url = get_permalink();

	if ( $attr['href'] == 'facebook' )
		$attr['href'] = 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode( $url );
	elseif ( $attr['href'] == 'twitter' )
		$attr['href'] = 'http://twitter.com/home?status=' . urlencode( $url );
	elseif ( $attr['href'] == 'google' )
		$attr['href'] = 'http://plus.google.com/share?url=' . urlencode( $url );
	elseif ( $attr['href'] == 'pinterest' )
		$attr['href'] = 'http://pinterest.com/pin/create/button/?url=' . urlencode( $url );

	return mycred_render_shortcode_link( $attr, $link_title );
}

If you want to limit so users only get points once for each “share”, you can set the “Points for clicking on links” to limit once for each URL.

Remember that you would need to add target=”_blank” for these type of links or just enforce it automatically:

add_shortcode( 'mycred_share_this', 'mycred_render_shortcode_share_this' );
function mycred_render_shortcode_share_this( $attr, $link_title )
{
	// Get URL (we assume you only use this shortcode inside the loop)
	$url = get_permalink();

	// Append social media share urls
	if ( $attr['href'] == 'facebook' )
		$attr['href'] = 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode( $url );
	elseif ( $attr['href'] == 'twitter' )
		$attr['href'] = 'http://twitter.com/home?status=' . urlencode( $url );
	elseif ( $attr['href'] == 'google' )
		$attr['href'] = 'http://plus.google.com/share?url=' . urlencode( $url );
	elseif ( $attr['href'] == 'pinterest' )
		$attr['href'] = 'http://pinterest.com/pin/create/button/?url=' . urlencode( $url );

	// Always make links open in a new window
	$attr['target'] = '_blank';

	// Pass it on
	return mycred_render_shortcode_link( $attr, $link_title );
}

 

Add Referral ID to the URL

You can combine this custom code and add in the users referral ID allowing them to earn points for users who use the shared link. Of course it requires you to have the “Points for referrals” hook enabled:

add_shortcode( 'mycred_share_this', 'mycred_render_shortcode_share_this' );
function mycred_render_shortcode_share_this( $attr, $link_title )
{
	// Get URL (we assume you only use this shortcode inside the loop)
	$url = get_permalink();

	// Add affiliate link (requires myCRED 1.5.3 or higher)
	$url = mycred_render_affiliate_link( array( 'url' => $url ) );
	// Append social media share urls
	if ( $attr['href'] == 'facebook' )
		$attr['href'] = 'https://www.facebook.com/sharer/sharer.php?u=' . urlencode( $url );
	elseif ( $attr['href'] == 'twitter' )
		$attr['href'] = 'http://twitter.com/home?status=' . urlencode( $url );
	elseif ( $attr['href'] == 'google' )
		$attr['href'] = 'http://plus.google.com/share?url=' . urlencode( $url );
	elseif ( $attr['href'] == 'pinterest' )
		$attr['href'] = 'http://pinterest.com/pin/create/button/?url=' . urlencode( $url );

	// Always make links open in a new window
	$attr['target'] = '_blank';

	// Pass it on
	return mycred_render_shortcode_link( $attr, $link_title );
}

 

Done

And there you go, now we have a custom shortcode that you can use to let your users gain points for “sharing” your posts or pages. If you have any further questions, feel free to post them below.

11