Black Friday and Cyber Monday Sale is here! Get 30% off using coupon code BFCM30

Awarding points for users who “Share” a post.

Home / Tutorials / 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.

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.

Download
  1. Hi. I’m using this function but it’s awarding points immediately when the link is clicked and not after the link has been shared. So I could really sit there and click the share button 100 times to gain points without actually sharing the link. How do I overcome that?

    Thanks heaps

    1. Since we are using the “Points for clicking on links” hook, you can enforce rules. If you visit your myCRED > Hooks page you will find that you can limit either by URL or ID. I would suggest you limit by URL since each share link will have a unique one.

  2. Thanks for your tutorial but i’m a real newbie…
    The last box is the final code ?
    And can you tell me where i have to put this code please???
    I’m lost!!!

    Thank you

    1. Hey Christopher.

      You can hook into the template tag parser and customize your log entry before myCRED does.

      add_filter( 'mycred_parse_log_entry_link_click', 'customize_link_click_log_entries', 10, 2 );
      function customize_link_click_log_entries( $content, $log_entry )
      {
      	// Start by checking the detail you need exists (backwards comp)
      	if ( ! isset( $log_entry->data['link_url'] ) ) return $content;
      	// The URL clicked on:
      	$url = $log_entry->data['link_url'];
      	// The ID used in the < a > element (if used)
      	$id = $log_entry->data['link_id'];
      	// The Link title < a > title < / a > (added in 1.3)
      	$title = $log_entry->data['link_title'];
      	
      	// Do your thing
      	
      	// Return result
      	return $content;
      }
      

      Note that the link_title is only available for log entries from when you updated to 1.3. For older entries this will not be available!

    2. I don’t understand what exactly i should do. Let me be more clear.

      I use the above shortcode for facebook share:

      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 );
      }
      

      I use “for clicking on links” hook this template tag:
      %plural% for clicking on link to: %url%

      What i want to do is to keep this template tag as general tag for links and only for the facebook share shortcode to change it like this “points for sharing on Facebook”. This will also appears in the box notification;

      Thank you!

    3. Yes. And to do that, the easiest option is to hook into myCRED and before the log entry is presented in your log or in your notice, alter the log entry to your needs.

      Using the code I showed you, you would hook in, and check that the URL (which is saved when a user clicks on something) is a facebook url. If it is, then you change the entry (see “Do your thing” in the code I showed you).

    4. Sorry but still i can’t do it. Can you give me please a full example?

      Thank you very much!