HTML
Adding Facebook Like Button To WordPress And Drupal Pages
October 30, 2010
0

First, get the Facebook code that you’d like to use. There are several variations described here:
https://www.permadi.com/blog/2010/10/adding-facebook-like-widget-button-to-your-web-pages/

An example code is shown below (depending on which options you chose, the code may look slightly different:

<iframe src="http://www.facebook.com/plugins/like.php?
href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&amp;layout=standard
&amp;show_faces=true&amp;width=450&amp;action=like
&amp;colorscheme=light&amp;height=80" scrolling="no"
frameborder="0" style="border:none; overflow:hidden;
width:450px; height:80px;" allowTransparency="true"></iframe>

The section to pay attention to is

href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike

This contains the URL to ‘like,’ and we will modify the value depending on which page you are at using two of the most popular CMS, Drupal and WordPress.

In WordPress

I am using WordPress 2.9.2 for this example and I am adding the Like button the a Single Post page.

In WordPress Admin, go to Appearance->Edit. The Theme Editor should open, then select Single Post (single.php) from the list of files. Paste the Like button code to where you want it to appear, but we still need to modify it to link to the current page.

We can use the get_permalink() function to get the URL of the current post.
http://codex.wordpress.org/Function_Reference/get_permalink.
Alternatively, instead of using get_permalink(), you calso use this function: wp_logout_url($_SERVER[‘REQUEST_URI’])

So,
In the Like button code, find this portion:

href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&amp;layout

Replace it with the URL of the blog post:

href=<?php echo esc_url(get_permalink());?>&amp;layout

The call to esc_url() is to ensure the page is properly encoded. For more about URL encoding, see here: https://www.permadi.com/tutorial/urlEncoding/

In Drupal

I am using Drupal 6 for this example.

Here, you can use the combination of the global variable $base_root and request_uri() function to get the current URL.

The request_uri() function returns the relative path to the page, such as /blog/post.
If you are editing a node template, you can also use the variable $node_url instead of request_uri().

You need to prefix $base_root into the request_uri() in order to get the full URL.

$base_root is a global variable and it will return the root of your drupal install, such as: http://www.example.com.

The Like button code can be added to in the node.tpl.php or other custom node template you want to add the button to (see: http://api.drupal.org/api/drupal/modules–node–node.tpl.php/6). Paste the Like button code to where you want it to appear, but we still need to modify it to link to the current page.

In the Like button code, find this portion:

href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&amp;layout

and replace it with:

href=<?php global $base_root; echo $base_root.check_plain(request_uri());?>&amp;layout

The check_plain() function ensures that the URL are encoded and does not contain weird characters.
For more about URL encoding, see here: https://www.permadi.com/tutorial/urlEncoding/