Customizing SMF > Tips and Tricks

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

(1/11) > >>

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) --- <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>
--- End code ---

--- Code: (replace with) --- <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>
--- End code ---

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) ---function template_html_below()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;

echo '
</body></html>';
}
--- End code ---


--- Code: (replace with) ---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>';
}
--- End code ---

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 :)

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?

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

4b11l:
Ah, I see. Thanks.

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.

Navigation

[0] Message Index

[#] Next page

Go to full version