Wednesday, January 25, 2012

[PHPBB MOD] Facebook like button in each topic / thread

This mod adds a facebook like button to each topic.
Tested with phpBB 3 (But there is no version dependent code, so it will work on all versions, if someone tests in other versions, please post the errors/success messages here)

Screenshot:


Steps:
1. OPEN FORUM_ROOT/includes/functions.php

2. FIND THE BLOCK:
--------------------
// Send a proper content-language to the output
    $user_lang = $user->lang['USER_LANG'];
    if (strpos($user_lang, '-x-') !== false)
    {
        $user_lang = substr($user_lang, 0, strpos($user_lang, '-x-'));
    }
--------------------

3. ADD BELOW:
--------------------
//Facebook like hack by Sriram.A.S. (sriramdasty7@gmail.com)
$pieces=explode("?",$user->page['page']);
if($pieces[0]=="viewtopic.php")
{
$curr_url=generate_board_url() .'/'. $pieces[0]."?";
$pieces=explode("&",$pieces[1]);
$flag_tmp=0;
foreach($pieces as $temp_x)
{
if(substr($temp_x,0,2)=="f=" && $flag_tmp==0)
{
$curr_url=$curr_url.$temp_x;
$flag_tmp=1;
break;
}
}
foreach($pieces as $temp_x)
{
if(substr($temp_x,0,2)=="t=" && $flag_tmp==1)
{
$curr_url=$curr_url."&".$temp_x;
break;
}
}
}
--------------------

4. In the same file, FIND
--------------------
'SITE_LOGO_IMG'            => $user->img('site_logo'),
--------------------

5. ADD BELOW
--------------------
'CURRENT_URL'      => $curr_url,
--------------------

6. SAVE file with changes

7. OPEN FORUM_ROOT/styles/{YOUR_TEMPLATE}/template/viewtopic_body.html

8. FIND FIRST OCCURANCE OF THE BLOCK
--------------------
<div class="buttons">
    <!-- IF not S_IS_BOT and S_DISPLAY_REPLY_INFO -->
--------------------

8. ADD BEFORE
--------------------
<div align="right">
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="{CURRENT_URL}" show_faces="true" width="450"></fb:like></div>
--------------------

9. If you want the like button also in bottom, fine the second occurance of the block and also add accordingly

10. SAVE file with changes

11. Now, Login to Administration Control Panel from frontend and in the main page,
Resynchronise or reset statistics-->Purge the cache-->Run now

If you face any problems, post here.

14 comments:

  1. Good work. It works fine and easy.

    ReplyDelete
  2. Hi. I have a question: when you "like" a topic, as it appears in facebook? I used a mod that, when I "like" a topic, the topic title does not appear in facebook.

    ReplyDelete
  3. Yeah, its because we pass only the URL to fb for like, and fb does the rest, anyways, nice suggestion, i will try to find a way to post topic name properly in fb. I will check that and upload new mod soon. Thanks

    ReplyDelete
  4. I can't find:
    // Send a proper content-language to the output
    $user_lang = $user->lang['USER_LANG'];
    if (strpos($user_lang, '-x-') !== false)
    {
    $user_lang = substr($user_lang, 0, strpos($user_lang, '-x-'));
    }
    in functions.php pls.

    ReplyDelete
  5. Hello friend. Im using grunge template, and i did everything you described. And nothing happens. It doesn't give me an error but nothing shows. And im sure that i did it right. Please help me.

    ReplyDelete
  6. Totally agree with your suggestion... Very nice post and good information here... Thanks for posting that....

    ReplyDelete
  7. Do you need a like button image for this ?

    ReplyDelete
  8. Ive done the file edits, but when i click like, nothing is appearing on my facebook wall?

    ReplyDelete
  9. Hi John, No image is required for this, could you plz give me a link to your phpBB forum here, I removed my phpBB setup, we will see whats the problem!!

    Thanks

    ReplyDelete
  10. Great job, this works as expected.

    Just one note that I found when trying the Facebook like button. If you are not currently signed into Facebook, and then you are on the Forum and use the "Like" button, you are taken to Facebook to log in, then once you are logged in, you still need to go back to your post in the forum to "like" it on Facebook.

    ReplyDelete
  11. Totally agree with your suggestion... Very nice post and good information here... Thanks for posting that....

    ReplyDelete
  12. Hi,

    "Yeah, its because we pass only the URL to fb for like, and fb does the rest, anyways, nice suggestion, i will try to find a way to post topic name properly in fb. I will check that and upload new mod soon. Thanks:"

    Have you got any idea for this yet?

    Also is there any way a user can click the like button even if he not logged into facebook?

    Ahh forgot to say, thanks for the good post!!!

    ReplyDelete
  13. I cant get access to my forum anymore. How can i fix it, i did as you suggested

    ReplyDelete
  14. May I know what is the error. You can revert all changes, may be you accidentally missed something. Anyways, if you need help, you can mail me too. Not to worry.

    ReplyDelete