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.

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.

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!

    1. Hi Jason.

      Just as with any other shortcode you use in WordPress you need to do two things:
      1. You need to paste the code presented in this tutorial into your themes functions.php file in order to register the shortcode for use.
      2. You enter the shortcode into your post’s or page’s content field when you edit the post.

  3. Hi Gabriel,
    Is it possible to hook Facebook like button? I want to increase post’s author point when someone like his/her post
    Many thanks and best regard

  4. Hi,

    I added the codes to my theme’s (Avada Version 3.1.1) function.php, and added the shortcode, which I assume is [mycred_share_this href=”facebook”] to a post. But All I got is a blank page.

    Am I missing something else?

    Thanks.

    Lack

    1. I forgot to add that if I just use the shortcode [mycred_share_this], I got this “myCRED error Anchor missing URL!”.

      Thanks again.

    2. A blank page means something went wrong. Enable WP_DEBUG in your wp-config.php file and check what error messages you get. If you have pasted the above code correctly, you can use the shortcode: [mycred_share_this href=”facebook”] in your post/page content.

    1. You enable WP_DEBUG in your wp-config.php file by changing it from false to true. Note that when enabling WP_DEBUG you will show ALL error messages. You just need to enable it, visit the page where you use the shortcode, check what error message you get, then disable WP_DEBUG again by changing it back to false and save.

    2. This is the message I got.

      Fatal error: Call to undefined function mycred_render_shortcode_link() in /home/content/63/11589863/html/lackdesign/wp-content/themes/sweetdate/functions.php on line 1313

    3. After re-installing mycred, I got this error message “myCRED error Anchor missing URL!”. the shortcode I use is [mycred_share_this href=”facebook”]

    4. Please contact me via the contact page and send me the code you pasted into your functions file as I suspect you have not entered the correct code into your functions.php file.

  5. Hey,

    This is what i am looking for.

    But dont wanna create shortcode to use post or page. I wanna add button my single.php. My site structure is required this.

    How could it be possible using this hook?

    1. You can insert the same shortcode into your single.php directly by using the do_shortcode function that comes with WordPress.

      <?php echo do_shortcode( '[mycred_share_this href="facebook"]' ); ?>
      
    1. Hey!

      In 1.3.3 I have added a new filter which would allow you to do this. Unfortunately in the current version of myCRED, you would need to replace the “Points for clicking on links” hook via your themes functions.php file.

    2. OK Thanks Gabriel,

      Another thing:

      I would like to have the Author to get also a point when someone shares the post.

    1. The code in this tutorial uses the mycred_link shortcode so you can not differentiate between clicking on links and sharing a post and due to that, don’t know which is which and if there is an author to get.

      Right now, the easiest way to do this would be to build a separate shortcode and ajax function that does this for you but the above code can not do what you are looking for.

  6. Nice but like i presumed the point is awarded upon clicking the share link. But the user can click on the link and close the window WITHOUT actually sharing it.
    It’d be great if you could find a way around it. You can look at how some “content locking” plugins does it – (i know i will).

  7. Okay, I don’t know if I’m doing something wrong, but I placed the following Share this article on [mycred_share_this href=”facebook”] | [mycred_share_this href=”twitter”] | [mycred_share_this href=”Pinterest”] on the bottom of a content post and what I get back is Share this article on | |

    I triple made sure I added the code to the theme function page, I’m fairly sure I’m using the shortcode correctly.

    1. Have you had a look at the HTML code that is rendered on the front page? See what HTML is actually output and not what the browser shows you (in this case nothing).

      When editing the post with this shortcode, check the plain text version and make sure you are using proper quotation marks (“) and not some special ones that are not rendered properly. The ones you used in your comment is not going to work.

  8. I keep trying to add in the shortcodes and it is not working. I placed the entire code into the functions.php file. The first time I tried to guess at what Reddit’s url would be but came back with errors so I just took it out and kept facebook, twitter, and google+. After this the site was fine again but the shortcodes would not work and just showed up as text in my posts.

    I did alter the functions.php file by adding the woocommerce coupon shortcode snippet you gave us a tutorial on. Could this be causing a problem?

    1. Hey.

      I am no expert on Reditt but I think it is:

      http://reddit.com/submit?url=yoururl&title=optionaltitle
      

      If all of the sudden the shortcodes just renders as text and you did not change the shortcodes in any way, then you must have made some sort of change in the functions.php file. Shortcodes returned as text often means that the shortcode is not registered. Check to make sure you did not remove:

      add_shortcode( 'mycred_share_this', 'mycred_render_shortcode_share_this' );
      
  9. Hello Gabriel, is there a way to make the $url = mycred_get_ref_for_url(). That way the referral link with ref id. That way users can get points for sharing the post with the referral link.

    1. Okay, I figured that would work, thank you. And one last question, is there a way to change the my_share_this function to display a button rather than just plain text?

    2. Hey Mark.

      You can via CSS styling make any element, including a link, to look like a button. Usually you remove the underlining, set a background color, optional border then adjust the elements size.

  10. This is how I pasted the code into my functions.php theme file, now when I use the shortcode nothing at all appears:

    function my_theme_wrapper_start() {
      echo '';
    }
    
    function my_theme_wrapper_end() {
      echo '';
    }
    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 );
    }
    
    1. You will need to enable WP_DEBUG in your wp-config.php file on your site and then visit the page where the shortcode is used to see what errors (if any) are thrown. Once you have your errors, you can disable WP_DEBUG again.

      Note that by enabling WP_DEBUG you will show ALL errors, not just myCRED related. But it should tell you what the issue is.

      I assume you have the myCRED plugin enabled while using the shortcode.

  11. Hello, I’ve been working on activating the points for sharing code, but it is not working. I’ve inserted the following shortcode into one of my pages: [ mycred_share_this href=”pinterest” ]
    and added this code snippet to my functions.php:

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

    When the social sharing button for pinterest is click, users aren’t receiving any points. What am I doing wrong?

    Thanks for your help!

    1. Hi.

      I would start by checking if the clicking on the link causes any javascript errors via your browsers console.

      Do they get points for all other clicks just not pinterest?

  12. Gabriel,

    I ran WP_debug and it didn’t provide any errors on the page. I am still not coming up with anything when I put the shortcode in. I tried facebook, twitter, and google. I have a social sharing plugin activated with sharing icons on the pages I am trying to insert the short code, does that make a difference?

    The MyCred pluggin is enabled.

    1. Hey Ryan.

      Very odd. Have you checked that if you look at the raw html code that is returned on the page where the shortcode should appear, there is no anchor elements inserted where they should be? Could it be that they are there but something (theme or plugin) is hiding it via CSS?

  13. sorry didn’t realize it was going to apply the code…


     
    It’s no secret our schools totally teach us a small percentage of the injustices our country carried out to the indigenous people of the northern Hemisphere.
    It’s no secret that hipsters remain the lowest form on the political spectrum.
    And it’s a sad day that hipsters are now carrying out the political transgressions of our forefathers. How many festivals can a generation really attend, high as shit, wearing a fake indian headdress down to their ass?’

  14. Ok so I assume you have not set a title for each link? Have a look in the documentation for the mycred_link shortcode to see how to use the shortcode.

    As explained in this tutorial, this custom shortcode we create is based on the mycred_link shortcode. So I assumed that if you know how to use the mycred_link shortcode, you know how to use this one. The only difference between the two is the URL which this shortcode sets for you.

  15. Sorry for the confusion. I figured it out finally! Thank you for your help. For anyone else having the same issue the entire shortcode you will enter into your posts will be:

    [mycred_share_this href=”facebook”]Facebook[/mycred_share_this]

    1. Hi.

      Where you place the code in the functions.php file is up to you and has no effect on the code. I would suggest you place it in the end of the file as a new section to keep things clean.

  16. Thank you.

    I wrote the code in function.php.

    Should I stick them what short code to where you want to install?

    I saw a sample, but I do not know.

    Could you write the code?

  17. Could it be, that this is broken in the latest version (i got beta 1.4 here)?
    If i’m using the shortcode, the correct html source is built. But by clicking on the link i don’t get points added.
    Maybe the jQuery scritp which should get called through

    // Make sure jQuery script is called
    $mycred_link_points = true;

    Doesn’t get called anymore?

    1. I did try it with:

      [mycred_href=”http://google.com” title=”” target=”” style=”” id=”” class=””]Link Title[/mycred_link]

      I also enabled the Points for clicking on links with 11.00 per link click, but no points get added.

      I thought there may be a javascript error, but there is no error shown in de dev tools.

      Do you have any ideas how i could debug it further?

    2. The shortcode you pasted here is not valid. There is no shortcode called mycred_href. Are you sure you are using mycred_link href and not what you pasted above?

    3. i am sorry there was something wrong with copy and pasting.
      my shortcode looks like this and gets rendered correctly, but the points are not added when clicking the link:

      [mycred_link href="http://google.com" title="" target="" style="" id="" class=""]Link Title[/mycred_link]

    4. I recently updated to 1.4 (was 1.4 beta before) and everything works again!
      Thank you!

  18. Hello,

    I would like to let my users get 10 points per share (or click on links), but being able to do this once every 24 hours.

    How can I achieve this? Thank you!

    1. Hey Christopher. This tutorial is base don the Points for clicking on links hook in myCRED so with that you can only enforce the rules set for this hook. You could hook into mycred_add and enforce a daily limit of some kind but it could be tricky to differentiate between which links they clicked on and have not clicked on yet.

    2. Thank you for your answer. I use this code but it doesn’t work properly.


      /* Limit epoints click on links per day */
      function daily_link_click_limit( $reply, $request, $mycred )
      {
      if ( $request['ref'] != 'link_click' || $reply === false ) return $reply;

      $max_per_day = 10;
      $today = date_i18n ( 'Y-m-d' );

      // Get users limit (add new if they do not have one)
      $limit = get_user_meta( $request['user_id'], 'daily_link_limit', true );
      if ( empty( $limit ) ) $limit = array( 'day' => $today, 'count' => 0 );

      // If we have reached our daily limit refuse this transaction
      if ( $limit['day'] == $today && $limit['count'] >= $max_per_day ) {
      return false;
      }

      // Else increment counter and save before approving
      $limit['day'] = $today;
      $limit['count'] = $limit['count']+1;

      update_user_meta( $request['user_id'], 'daily_link_limit', $limit );
      return $reply;
      }
      add_filter( 'mycred_add', 'daily_link_click_limit', 10, 3 );

    1. Hi.

      Exactly what I say in the beginning of this tutorial! You get points for clicking on the link, not for sharing. If you want to award points for sharing you will need to setup a Facebook app.

  19. I’m using an open graph with sharing icons short code that pops up into a new page before it is sent. Is it still possible to use this coding in combination with that type of short code?

  20. Hi Gabriel, super awesome plugin, thank you for all your time building it! I have a question about the code. When I tried to implement this tutorial it wasn’t working. The console log from links.js was printing a 0. After further examination of the code I noticed that the reason was because of the ajax function in mycred-module-hooks.php called ajax_call_link_points(). There is a line that is supposed to assign the variables of $token by using the list() function, the problem occurred after that assignment when $type was not validated as a type, it was actually a colon(:). Was $token supposed to be an array at this point? I ask because I noticed $token was a string with values separated by colons. Is this another way to use list(), or am I missing something in my implementation that didn’t explode $token into an array?

    1. Hey Ryan.

      If you inspect the link element that is generated (that is supposed to give points) is the data-token attribute of the anchor element encoded or is it a : separated list?

      Also, if you try using the mycred_link shortcode you get the same issue?

    2. Looks like the token isn’t being exploded because mycred_protect() is returning false. So it looks like my server might have mcrypt disabled. I will talk to my sys admin. I’ll let you know if thats not the case though. Thanks again for everything!!

  21. Hello Sir, I am a WordPress Developer. I want to use Your Plugin for Very Nice Rewarding Management System. But My Requirement is quite different. The Requirement is that, suppose some user shared a link through a Social Media (Like Facebook, Twitter etc.). For this sharing, he/she will not be rewarded. Now, while someone clicks on this Shared link from the social media and visits site, then the User (Who shared the link) will be rewarded. Please Let Me know whether it is possible throughout Your Plugin.

    1. Hey.

      Have you had a look at the Affiliates Hook? It’s called “Points for referrals” which you can use to award points to users who link to your website using their referral code. It’s a very basic referral system that allows you to award points for visits and signups.

      You could adjust your share links that are used in this tutorial and append the referral ID for users so when they click on the “Share This” link that this tutorial creates, it automatically adds in the referral ID.

    2. hi Gabriel,
      i also have such request for my site ‘Add Referral ID to the URL’, so i able to give points to social users after they view my site shared post(link) or register as member to gain points on my site instead of just count the shared link, and i was told and guided by the social button plugin vendor that should just need to put a line of your code snippet into its script file of the location he mentioned:
      $url = mycred_render_affiliate_link( array( ‘url’ => $url ) );

      but it seem like it can not be works after added the code snippet into the script file, so may i have your assisting for what i was miss out or anything may be goes wrong?
      (see near the bottom sentence that have stated ‘//giore add mycred affiliate code’ which i insert your code snippet)

      // custom_share_message_address
      $post_essb_post_share_message = $postMetaOptions['essb_post_share_message']; //get_post_meta($post->ID, 'essb_post_share_message', true);
      $post_essb_post_share_url = $postMetaOptions['essb_post_share_url']; //get_post_meta($post->ID, 'essb_post_share_url', true);
      $post_essb_post_share_image = $postMetaOptions['essb_post_share_image']; //get_post_meta($post->ID, 'essb_post_share_image', true);
      $post_essb_post_share_text = $postMetaOptions['essb_post_share_text']; //get_post_meta($post->ID, 'essb_post_share_text', true);
      $post_essb_post_fb_url = $postMetaOptions['essb_post_fb_url']; //get_post_meta($post->ID, 'essb_post_fb_url', true);
      $post_essb_post_plusone_url = $postMetaOptions['essb_post_plusone_url']; //get_post_meta($post->ID, 'essb_sidebar_pos', true);

      $post_essb_twitter_username = $postMetaOptions['essb_post_twitter_username']; //get_post_meta($post->ID, 'essb_post_twitter_username', true);
      $post_essb_twitter_hastags = $postMetaOptions['essb_post_twitter_hashtags']; //get_post_meta($post->ID, 'essb_post_twitter_hashtags', true);
      $post_essb_twitter_tweet = $postMetaOptions['essb_post_twitter_tweet']; //get_post_meta($post->ID, 'essb_post_twitter_tweet', true);

      $post_essb_as = $postMetaOptions['essb_as']; //get_post_meta($post->ID, 'essb_as', true);

      $salt = mt_rand ();

      // show buttons only if post meta don't ask to hide it, and if it's not a shortcode.
      if ( $show_me ) {

      // @since 1.3.9.6 - check if it is shortcode then options by bp will not be executed otherwise we correct state with possible values from settings
      if ($is_shortcode) {
      $this->options_by_bp_active = false;
      }
      else {
      $this->set_options_by_bp_activate_state();
      }

      // texts, URL and image to share
      $text = esc_attr(urlencode($post->post_title));
      $url = $post ? get_permalink() : ESSBOptionsHelper::get_current_url( 'raw' );
      if ($this->avoid_next_page) {
      $url = $post ? get_permalink($post->ID) : ESSBOptionsHelper::get_current_url( 'raw' );
      }

      //giore add mycred affiliate code
      $url = mycred_render_affiliate_link( array( 'url' => $url ) );

      //$url = urlencode(get_permalink());
      if ( $is_current_page_url ) {
      $url = ESSBOptionsHelper::get_current_url( 'raw' );
      }

      if (!$is_shortcode) {
      $force_wp_fullurl = isset($options['force_wp_fullurl']) ? $options['force_wp_fullurl'] : 'false';
      if ($force_wp_fullurl == 'true') {
      //if (ESSBOptionsHelper::exsitQueryString()) {
      $url = ESSBOptionsHelper::curPageURL();
      //}
      }
      }

      if you want to have a look for the whole script file, i can send it to you.

      many thanks

  22. Hi! When I paste the code into functions.php I have a white space with the same shortcode written on the top of my back and front end. I assume I´m doing something wrong
    but what?

    Thanks

    1. Did you paste in the code with the <?php and ?> ?

      Remove them if you have and make sure you paste the code inside these brackets in your theme’s function.php file. If you paste them outside, then you will see the code itself on all pages.

  23. I pasted the code to my functions.php
    I added
    to

    to my single.php
    but then I receive below warnings when viewing a post. Any idea what is causing that?


    Warning: Illegal string offset 'href' in /functions.php on line 265

    Warning: Illegal string offset 'href' in /functions.php on line 267

    Warning: Illegal string offset 'href' in /functions.php on line 269

    Warning: Illegal string offset 'href' in /functions.php on line 271
    error Anchor missing URL!

    1. Hey. Your code never got pasted in. Please use a pastebin to show how you are using this shortcode. Remember that using this shortcode you must follow the same requirements as with mycred_link. So the href=”” attribute must be set in your shortcode!

    2. Hi Gabriel, I have the same problem lik this. What shoul I do to fix it. Thank you

  24. Oops, sorry, indeed I forgot the href attribute. No warnings anymore. But I’m using easy social share buttons plugin and is not working. No points given. Worth the try anyway 🙂

  25. I put all the code you provided to us.

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

    but as I can make links are displayed facebook , twitter , etc , so that the user can click

  26. Excuse me here do i have to firstly install the wordpress share this plugin befoe this code i will paste in my function.php works.
    or there is no need to install any plugin just install and it works

Leave a Reply

Last edited October 28, 2016