Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: JBlaze on June 12, 2011, 06:25:57 PM

Title: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: JBlaze on June 12, 2011, 06:25:57 PM
If you're like me, and just like having social media options on your site, well then check this out.

If you want to add quick social sharing buttons from Google, Facebook, and Twitter to all your posts, just add this code in your theme's Display.template.php. Note that if you have a custom theme without its own Display.template.php, you only need to modify the one in the default directory.

Code (find) Select
<h3 class="catbg">
<img src="', $settings['images_url'], '/topic/', $context['class'], '.gif" align="bottom" alt="" />
<span id="author">', $txt['author'], '</span>
', $txt['topic'], ': ', $context['subject'], ' &nbsp;(', $txt['read'], ' ', $context['num_views'], ' ', $txt['times'], ')
</h3>

Code (replace with) Select
<h3 class="catbg">
<img src="', $settings['images_url'], '/topic/', $context['class'], '.gif" align="bottom" alt="" />
<span id="author">', $txt['author'], '</span>
', $txt['topic'], ': ', $context['subject'], ' &nbsp;(', $txt['read'], ' ', $context['num_views'], ' ', $txt['times'], ')

<!-- Facebook -->
<span class="floatright" style="margin: 6px;" id="facebook">
<div id="fb-root"></div>
<fb:like href="', $scripturl, '?topic=', $context['current_topic'], '" send="false" layout="button_count" width="100" show_faces="false" font=""></fb:like>
</span>

<!-- Twitter -->
<span class="floatright" style="margin: 6px;" id="twitter">
<a href="http://twitter.com/share" class="twitter-share-button" data-url="', $scripturl, '?topic=', $context['current_topic'], '" data-text="CUSTOM_TWEET_TEXT" data-count="horizontal" data-via="PRIMARY_TWITTER_ACCOUNT" data-related="SECONDARY_TWITTER_ACCOUNT">Tweet</a>
</span>

<!-- Google Plus -->
<span class="floatright" style="margin: 6px;" id="gplus">
<div class="g-plusone" data-size="medium" data-annotation="inline" data-width="120" data-href="', $scripturl, '?topic=', $context['current_topic'], '"></div>
</span>
</h3>


Now, in order to use Facebook, Twitter, and Google Plus buttons, you'll need to add this to your index.template.php. You will need to modify this for every theme you wish to use the buttons on.

Code (find) Select
function template_html_below()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;

echo '
</body></html>';
}


Code (replace with) Select
function template_html_below()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;

echo '
<!-- Facebook Share -->
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>

<!-- Twitter Share -->
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

<!-- Google Plus -->
<script type="text/javascript">
  (function() {
var po = document.createElement(\'script\'); po.type = \'text/javascript\'; po.async = true;
po.src = \'https://apis.google.com/js/plusone.js\';
var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>

</body></html>';
}


You can replace the following vars with what you like:
CUSTOM_TWEET_TEXT = The text you want to tweet. Things such as "Check this out: ', $context['subject'], '" work nicely. You can alternatively leave it empty to use the page's title.
PRIMARY_TWITTER_ACCOUNT = The primary account you want to make the tweet via. Leave out the @.
SECONDARY_TWITTER_ACCOUNT = Same as above, but you can leave this one empty.

Screenshot:
(http://i.imgur.com/3hLOW.png)

It's that simple really. This is mainly for those who just want a quick hack without clouding up their installed mods list. :)

NOTE: This code is provided "as-is", and no support will be given as I do not have the time. Thank you for understanding :)
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: 4b11l on June 16, 2011, 06:38:12 PM
Hello, does this tip only post to one account such as the website account? Or is it like other service where the user is able to login their details and post to their twitter?
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: JBlaze on June 16, 2011, 06:45:44 PM
Quote from: 4b11l on June 16, 2011, 06:38:12 PM
Hello, does this tip only post to one account such as the website account? Or is it like other service where the user is able to login their details and post to their twitter?
It allows you to input the account you wish for people to follow as well as a secondary account for people to follow. It does not however make tweets through any given account. It just allows member to tweet share the link.
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: 4b11l on June 16, 2011, 06:51:26 PM
Ah, I see. Thanks.
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: 4b11l on June 17, 2011, 02:36:54 AM
I just tested it out and it was exactly what I was looking for! Thanks JBlaze, I think it was just that I didn't know how to explain it that I was confused in it's purpose.

It popped up a login for you to login your twitter details and it'll post to that account the custom message and link. I thought it automatically posted to the account listed as the primary.
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: JBlaze on June 17, 2011, 03:07:04 AM
No problem :)
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: Robert. on June 17, 2011, 04:47:55 AM
Thanks for the tip JBlaze :)
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: Masterd on June 17, 2011, 06:25:24 AM
There's one similiar trick.

http://www.simplemachines.org/community/index.php?topic=412141.0 (http://www.simplemachines.org/community/index.php?topic=412141.0)
Title: Re: [TIP/TRICK] Adding Twitter "Share" Button To Posts
Post by: lucas-ruroken on June 19, 2011, 09:14:10 PM
easy and useful ;) Thanks
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: JBlaze on June 22, 2011, 01:09:44 PM
! Added Facebook Like button
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Mbravo on July 01, 2011, 08:52:23 AM
Thanks for increase my knowledge ,it such a wonderful idea for sharing.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: MarcusJ on July 01, 2011, 02:47:08 PM
Can someone please explain how I can use the following from  or something similar to place the Google +1 button along side the Twitter and Facebook icons?  I have tried myself, but can't get it to work.   :-\

Also as a side note.  My forum is set to a fixed width of 1024px.  Any title with more than a few words causes the twitter button to vanish, but the Facebook like button remains.



if ($message['id'] == $context['topic_first_message'] )
{
echo '<g:plusone href="' . $scripturl . '?topic=' . $context['current_topic'] . '" size="small"></g:plusone>
<script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>';
}

      


Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: 8hoursale on July 23, 2011, 10:19:42 AM
I have SMF 1.1 and the Display Template looks a little different than this one does. Can this code be modified to work with 1.1?

Thank you!
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: aelmiger on July 27, 2011, 12:17:20 AM
Hello friend, thank you very much for any modification, but it happens that when I give a twitter, will not be adding the counter. How I can do to make this go adding the twitter done?

Thank you very much for the help

Sorry for my bad english
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: mijo on August 01, 2011, 04:39:32 PM
How to modify to make this work in 1.1.14 or is there some ready TIP to add Facebook Like button in 1.1.4..
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Monkey50 on August 04, 2011, 12:09:21 PM
how do i find the folder and how wouold i add google plus with that.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Adrek on August 05, 2011, 06:24:16 AM
Nice tip, but i have one OT question. What's the name of this theme (from screenshot from first post) ?
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: JBlaze on August 05, 2011, 06:52:31 AM
Quote from: ph4ntom on August 05, 2011, 06:24:16 AM
Nice tip, but i have one OT question. What's the name of this theme (from screenshot from first post) ?
It's a modified version of Dziner's Inferno theme.

Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Hasanudin on August 07, 2011, 11:04:28 AM
thank you :)
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: globalwalyy on September 19, 2011, 05:08:48 AM
hello thanks for this script, but i immediately i did this it destroy everything i have to rest it back pls help me out i think am lost somewhere
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: JBlaze on September 19, 2011, 08:53:17 AM
Quote from: globalwalyy on September 19, 2011, 05:08:48 AM
hello thanks for this script, but i immediately i did this it destroy everything i have to rest it back pls help me out i think am lost somewhere
Destroy everything how? Have a screenshot?
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: ~Dragon~ on October 19, 2011, 05:18:47 AM
Thank you so much tutorial posts
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: stang7043 on November 03, 2011, 01:52:27 PM
Quote from: mijo on August 01, 2011, 04:39:32 PM
How to modify to make this work in 1.1.14 or is there some ready TIP to add Facebook Like button in 1.1.4..

I am looking for the same ... Did anyone let you know how to modify this for 1.1.14 ?
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Sir Charles on December 10, 2011, 07:02:44 PM
Is there a way of using this in Simpleportal to display on articles? It works great on my topics but I would like them on my portal articles too if possible.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Jeet Chowdhury on January 10, 2012, 02:45:50 AM
see this:

http://muskilashan.com/index.php?topic=838.0

horizontal scroll bar come in browser with this code....

how to remove this unnecessary scroll bar?
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Jeet Chowdhury on January 10, 2012, 01:02:24 PM
bump...
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Home 4 Answers on February 01, 2012, 01:49:19 AM
For the FB like do I need to add the og tags?
If so any heads up on accomplishing this?

Thanks heaps!
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: -[Reece]- on February 01, 2012, 07:14:11 AM
Very nice, I'm now able to remove two mods!

Thanks a bunch.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Farmacija on February 04, 2012, 02:46:56 PM
it works, thank you!

For adding gplus button beside twitter and facebook


<span class="floatright" style="margin: 6px;" id="gplus">
<g:plusone href="' . $scripturl . '?topic=' . $context['current_topic'] . '" size="medium"></g:plusone>
<script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>
</span>
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: userquote on February 19, 2012, 10:46:17 PM
Thank you. It worked beautifully at my website.  :P
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: glennk on April 13, 2012, 02:24:57 PM
Hi guys, this is good. But I have a problem. Once I click like, it opens a box for me to comment ?? However the box can hardly be seen as it stays within the restraints of the blue bar, ie the blue bar does not expand to the size of the comments box.

Also Is there a way to make the icons bigger and have them somewhere more prominant, perhaps within the post just before the opening sentence ??
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: BinkyM on April 24, 2012, 12:46:43 PM
Quotejust add this code in your theme's Display.template.php

I hate sounding like a noob, but in this case, I'm an SMF noob. The theme I created doesn't have its own Display.template.php. Should it? If so, where should I grab it from?

Thanks for the tip,

Binky
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: TheListener on April 24, 2012, 12:51:25 PM
Quote from: BinkyM on April 24, 2012, 12:46:43 PM
Quotejust add this code in your theme's Display.template.php

I hate sounding like a noob, but in this case, I'm an SMF noob. The theme I created doesn't have its own Display.template.php. Should it? If so, where should I grab it from?

Thanks for the tip,

Binky

If the theme you use doesn't have its own display.template then try the default one.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: BinkyM on April 28, 2012, 02:46:37 PM
Thanks, Fossil. Will do! I'm getting better at bumbling around at a rapid pace!

Binky
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: psycho180 on April 30, 2012, 04:24:12 AM
Is there any mod avlble for this post???

i tried but didn't work check it out plz help me sir...
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: psycho180 on April 30, 2012, 05:02:57 AM
hey thnks man i did... but facing little bit prbm...

adding screenshot help me out plz

(http://2.bp.blogspot.com/-GphupbT50RY/T55Uq0JRiCI/AAAAAAAADfc/1xcTmbT5chM/s1600/jobsalert.net.png)
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: BinkyM on April 30, 2012, 03:18:00 PM
Wow, JBlaze; it looks really nice in your screenshot, but all I get it the word "Tweet," and that's it. I'm still on my hunt for Facebook and Twitter buttons!

(Not that my users even *use* these things, but I'm trying to drag us all into the 21st century.) (I only just created a FB page a couple months ago, and it's a fake one. I needed it for some reason or another.) (I haven't anything mundane enough to tweet.)

Binky
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Andrew103 on May 05, 2012, 08:41:40 PM
Did you have a code for Google +1 ? thanks for code of twitter and facebook it really help to my website what I need is now Google +1 and other coding.




Pools Above Ground
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: TheListener on May 05, 2012, 08:48:15 PM
Hi Andrew103

Welcome to smf.

Would THIS (http://custom.simplemachines.org/mods/index.php?mod=3053) help?
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: cyberhat on May 20, 2012, 09:23:10 AM
thanks for the share
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: alchimia on May 28, 2012, 03:09:09 PM
Quote from: JBlaze on August 05, 2011, 06:52:31 AM
Quote from: ph4ntom on August 05, 2011, 06:24:16 AM
Nice tip, but i have one OT question. What's the name of this theme (from screenshot from first post) ?
It's a modified version of Dziner's Inferno theme.
Can we have this modified version?
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: simon36 on June 06, 2012, 06:01:59 PM
Does anyone have the code all together to add facebook, twitter and g+1 on to the last post?

Thanks in advance.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: 6Scylla on June 07, 2012, 07:03:48 AM
Awesome trick, Thanks abunch!
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: JBlaze on June 10, 2012, 01:39:12 AM
Updated to include Google Plus, as well as neatening up and clarified a few things.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: graaywolf on June 11, 2012, 02:06:07 PM
J...

GraayWolf here

added your edits and it works fine except the facebook share window is half hidden... you have to guess that what your typing is not a typo lol....

no attachments option I guess so I can't give you a screenshot... come into my stickam chat and i'll show ya what's happening....

Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: JBlaze on June 12, 2012, 09:28:33 AM
Quote from: graaywolf on June 11, 2012, 02:06:07 PM
J...

GraayWolf here

added your edits and it works fine except the facebook share window is half hidden... you have to guess that what your typing is not a typo lol....

no attachments option I guess so I can't give you a screenshot... come into my stickam chat and i'll show ya what's happening....

That's because the share window is bigger than the containing div. If it were made any bigger, it would throw off the layout and make things looks like hell. There' no real way to fix it that I can think of.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: kimjhamilton on January 15, 2013, 06:34:47 PM
Don't suppose you have the coding to share to Tumblr as well, cause that would be awesome.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: GraphicJunki on April 01, 2013, 03:10:20 PM
This worked for me on most of my themes.  Great tip  thanks. :)
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: labyo on April 07, 2013, 02:19:08 AM
Thanks  :D
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Nickyy on November 08, 2013, 05:04:09 AM
Thank you JBlaze for the tip/trick :)
I have
SMF Package       SMF 1.1.19
Sources                    2.0    1.1.19
Default Templates    2.0    1.1.12
Language Files    2.0    1.1.18
Current Templates    2.0    1.1

The result from adding the code is only a hyperlink to twitter with the word 'Tweet'. Screenshot:

Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: LePhuongBao on December 06, 2013, 01:59:11 AM
Thank you so much tutorial posts
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: eric1234 on February 26, 2014, 03:37:50 PM
For some reason it work well in Firefox but in Google Chrome it seem to be malfunction.  :(
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Paracelsus on March 06, 2015, 09:06:47 AM
Ok, here's a question: if you want to put a facebook/google+/twitter share link to a POST (not a whole topic or topic page) how to do it?
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Paracelsus on March 06, 2015, 04:10:23 PM
Ok, I can answer myself to this one... ;D

In the end I could achieve a working link for each post by using:
', $scripturl, '?topic=', $context['current_topic'], '.msg' . $message['id'] . '%23msg' . $message['id'] . '

That code is to be put right after the desired facebook/twitter/google+/etc link, for example:
https://www.facebook.com/sharer/sharer.php?u=', $scripturl, '?topic=', $context['current_topic'], '.msg' . $message['id'] . '%23msg' . $message['id'] . '"

You can check it out how it works live here: http://www.forumscp.com/index.php?topic=64983.20 (notice the share icon on top right-corner of each post)
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: Johnson12 on April 28, 2015, 08:53:03 PM
Thank you for your tips adding share button :)
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: njtweb on June 10, 2015, 03:57:44 PM
Quote from: Nickyy on November 08, 2013, 05:04:09 AM
Thank you JBlaze for the tip/trick :)
I have
SMF Package       SMF 1.1.19
Sources                    2.0    1.1.19
Default Templates    2.0    1.1.12
Language Files    2.0    1.1.18
Current Templates    2.0    1.1

The result from adding the code is only a hyperlink to twitter with the word 'Tweet'. Screenshot:

Any help on this? I have the same problem. I added this today on the "Flatline" theme. I edited the default "display.template.php" file because my theme doesn't have one. Then I edited my themes "index.template.php" file, as the OP instructs to do so.

The result....Just the text "Tweet", that's it.

Any help is greatly appreciated.
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: njtweb on June 10, 2015, 06:34:41 PM
Quote from: njtweb on June 10, 2015, 03:57:44 PM
Quote from: Nickyy on November 08, 2013, 05:04:09 AM
Thank you JBlaze for the tip/trick :)
I have
SMF Package       SMF 1.1.19
Sources                    2.0    1.1.19
Default Templates    2.0    1.1.12
Language Files    2.0    1.1.18
Current Templates    2.0    1.1

The result from adding the code is only a hyperlink to twitter with the word 'Tweet'. Screenshot:

Any help on this? I have the same problem. I added this today on the "Flatline" theme. I edited the default "display.template.php" file because my theme doesn't have one. Then I edited my themes "index.template.php" file, as the OP instructs to do so.

The result....Just the text "Tweet", that's it.

Any help is greatly appreciated.

Turns out, my company is blocking twitter and fb. I got home and both show in the post, but no G+
Title: Re: [TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts
Post by: gusu on September 30, 2018, 01:58:59 PM
I'm using the Avada theme. Can I implement this technique safely on it?