0

Is there any way that I can code custom buttons for liking, sharing, and tweeting on individual posts on wordpress. If so, how would I link the like button to like the post on Facebook and how would I link the share and tweet buttons.

  • Your question is overly broad. You need to be more specific with your issue. Where is your code and where are you stuck. File an edit and provide your current code and point out exactly where your code is failing you. Thnk you – Pieter Goosen Jun 19 '15 at 7:17
1

Here is a simple way to add custom sharing buttons (just place this code where you want your sharing buttons to be displayed):

<?php
$twitter_username = 'your_twitter_username';
$shortURL = esc_url(get_permalink());
$shortTitle = esc_attr(get_the_title());
$imgURL = wp_get_attachment_url( get_post_thumbnail_id() );
$twitterURL = 'https://twitter.com/intent/tweet?text='.$shortTitle.'&amp;url='.$shortURL.'&amp;via='.$twitter_username;
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$shortURL;
$googleURL = 'https://plus.google.com/share?url='.$shortURL; 
$pinURL = 'http://pinterest.com/pin/create/button/?url='.$shortURL.'&media='.$imgURL.'&description='.$shortTitle;
?>

<a class="facebook-btn" href="<?php echo $facebookURL; ?>" target="_blank" onclick="window.open('<?php echo $facebookURL; ?>', 'popupwindow', 'scrollbars=no,width=500,height=400');return true">Share on Facebook</a>
<a class="google-btn" href="<?php echo $googleURL; ?>" target="_blank" onclick="window.open('<?php echo $googleURL; ?>', 'popupwindow', 'scrollbars=no,width=500,height=400');return true">Share on Google Plus</a>
<a class="twitter-btn" href="<?php echo $twitterURL; ?>" target="_blank" onclick="window.open('<?php echo $twitterURL; ?>', 'popupwindow', 'scrollbars=no,width=500,height=300');return true">Tweet on Twitter</a>
<a class="pinterest-btn" href="<?php echo $pinURL; ?>" target="_blank" onclick="window.open('<?php echo $pinURL; ?>', 'popupwindow', 'scrollbars=no,width=700,height=500');return true">Pin on Pinterest</a>

Now what you need to do is to add some style to these classes:

.facebook-btn

.google-btn

.twitter-btn

.pinterest-btn

Hope this helped you.

  • You should escape the title and the URLs. Both are not safe! – fuxia Jun 19 '15 at 7:35
  • @toscho It is correct like this? I really don't know how to escape the title and the URLs... and I don't know why I should do this either... – Nita Jun 19 '15 at 7:50
  • The title can contain markup, even a script. You could also use the_title_attribute([ 'echo'=>false]) for that. The image URL should be escaped too. – fuxia Jun 19 '15 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.