I want to add facebook like button to my page. I wrote the following code. It works fine.

<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like show_faces="true" width="450"></fb:like>

But i want to add the button through drupal_add_js() function. I wrote the following code but it did not work.

$myscript1 = '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>';
echo drupal_add_js($myscript, 'inline');

What is the problem here?

[Edited to add <code> and </code> tags: nevets]

Comments

nevets’s picture

Where did you put the code?

There should be no 'echo' before drupal_add_js()

And where do you add the <fb:like show_faces="true" width="450"></fb:like>, it is not shown in the second part.

hmdnawaz’s picture

Where should i place the <fb:like show_faces="true" width="450"></fb:like>.

I wrote this code while creating a page.

Ahmad Nawaz
Acquia Certified Developer
Email: hmdnawaz@gmail.com
Skype: hmdnawaz

hmdnawaz’s picture

If someone have idea about this please help me.
I am waiting for your replies.

Ahmad Nawaz
Acquia Certified Developer
Email: hmdnawaz@gmail.com
Skype: hmdnawaz

rlnorthcutt’s picture

You have the basic idea right, but are just a bit off with your execution:

$fb_script = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
drupal_add_js($fb_script, 'external');

The above will add the fb javascript to your pages... and if you are clever, you will have some type of IF statement to only add them when needed (like using hook_nodeapi() in D6 or hook_entity_prepare_view() in D7... or in preprocess for your theme)

However, you still need to add the code for displaying the like box:

<fb:like show_faces="true" width="450"></fb:like>

This could just be added to your theme OR (even better) add it to a block and then you can stick it wherever you like.

regards,
Ron Northcutt
Directory of Technical Marketing, Acquia

jenlampton’s picture

1) in template.php I added this function... (don't forget to escape your quotes, the filter here is stripping that out for some reason)

function mytheme_process_html(&$variables) {
  // Add the Facebook SDK.
  $variables['page_top'] .= "<div id=\"fb-root\"></div>
  <script>(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = \"//connect.facebook.net/en_US/all.js#xfbml=1&appId=YOURAPPIDHERE\";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, \'script\', \'facebook-jssdk\'));</script>";
}

2) in my node.tpl.php I added this code...

<div class="fb-like" data-href="https://www.example.com" data-width="350" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>