News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

[TIP/TRICK] Adding Twitter and Facebook "Share/Like" Button To Posts

Started by JBlaze, June 12, 2011, 06:25:57 PM

Previous topic - Next topic

JBlaze

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:


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 :)
Jason Clemons
Former Team Member 2009 - 2012

4b11l

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?

JBlaze

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.
Jason Clemons
Former Team Member 2009 - 2012


4b11l

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.

JBlaze

Jason Clemons
Former Team Member 2009 - 2012

Robert.



lucas-ruroken

Adk Portal 3.1 is coming....

Design your universe!

JBlaze

Jason Clemons
Former Team Member 2009 - 2012

Mbravo

Thanks for increase my knowledge ,it such a wonderful idea for sharing.

MarcusJ

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

      



8hoursale

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!

aelmiger

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

mijo

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..

Monkey50

how do i find the folder and how wouold i add google plus with that.

Adrek

Nice tip, but i have one OT question. What's the name of this theme (from screenshot from first post) ?
Polskie wsparcie SMF na simplemachines.org

the simplest solution is most likely the right one

JBlaze

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.

Jason Clemons
Former Team Member 2009 - 2012


globalwalyy

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

JBlaze

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?
Jason Clemons
Former Team Member 2009 - 2012

~Dragon~

Please do not PM or email, Thank respond please post fast to SMF!

stang7043

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 ?

Sir Charles

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.

Jeet Chowdhury

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?


Home 4 Answers

For the FB like do I need to add the og tags?
If so any heads up on accomplishing this?

Thanks heaps!

-[Reece]-


Farmacija

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>
www.farmaceuti.com
www.farmaceuti.com/tekstovi

userquote


glennk

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 ??

BinkyM

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

TheListener

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.

BinkyM

Thanks, Fossil. Will do! I'm getting better at bumbling around at a rapid pace!

Binky

psycho180

Is there any mod avlble for this post???

i tried but didn't work check it out plz help me sir...

psycho180

hey thnks man i did... but facing little bit prbm...

adding screenshot help me out plz


BinkyM

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

Andrew103

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

TheListener



alchimia

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?

simon36

Does anyone have the code all together to add facebook, twitter and g+1 on to the last post?

Thanks in advance.


JBlaze

Updated to include Google Plus, as well as neatening up and clarified a few things.
Jason Clemons
Former Team Member 2009 - 2012

graaywolf

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....

I'm trying...

-=(GW)=-

JBlaze

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.
Jason Clemons
Former Team Member 2009 - 2012

kimjhamilton

Don't suppose you have the coding to share to Tumblr as well, cause that would be awesome.

GraphicJunki

This worked for me on most of my themes.  Great tip  thanks. :)


Nickyy

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:



eric1234

For some reason it work well in Firefox but in Google Chrome it seem to be malfunction.  :(

Paracelsus

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?

Paracelsus

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)

Johnson12


njtweb

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.

njtweb

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+

gusu

I'm using the Avada theme. Can I implement this technique safely on it?

Advertisement: