WordPress – Making a Facebook plugin


Deprecated: Function create_function() is deprecated in /home/vhosts/poweredbygnulinux.com/wp-content/plugins/codecolorer/lib/geshi.php on line 4698

WordPress has a lot of plugins. but sometimes we would like add your custom functions to wordpress.

For these functions  we can create a new plugin.

We must write our plugin using WordPress hooks and PHP language.

We must activate the plugin , so wordpress runs it when the site is loaded. The process is similar to childtheming.

First step – create a folder

Create a folder inside  /wp-content /plugins/

For example, /my-facebook-plugin

 $ mkdir my-facebook-plugin

Step Two – Create a php file

Inside your new  folder -created in  the first step – with your favorite text editor, create the file my-facebook-plugin.php
The header defines wordpress plugin information used in the administration menu

/*
Plugin Name: My Facebook plugin
Description: My first plugin
Author: My name
Version: 1.0
Plugin URI: www.yourwebsite.com/plugin
*/

The lines are displayed in the  wordpress themes menu. Now you can activate the theme in admin panel.

Step Three – Create a function

We need to write a function with our custom code .

function WPFacebook_button($content)
{
global $post;
$link= js_escape(get_permalink($post->ID));

// agregar el contenido del post para el return
$content = $post ->post_content;

$button = '<iframe src="http://www.facebook.com/plugins/like.php?href='. $link . '&amp\
;layout=button_count&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp" styl\
e="overflow:hidden;width:100%;height:80px;" scrolling="no" frameborder="0" allowTranspar\
ency="true"></iframe>'
;

return $button . $content ;
}

I will explain the code

1) This line defines our new  function , it will give back the post content , in the var $content

function WPFacebook_button($content)

This line, calls the global variable $post, this var has the post content and a lot of information.

global $post;

This links leaves you to the main reference for this var :

 

Here , I list some of the most important contents in $post variable:

 http://codex.wordpress.org/Function_Reference/$post

ID
(integer) The post ID.

post_author
(integer) The ID of the post author.

post_date
(string) The post date using the server’s current timezone. Format: yyyy-mm-dd hh:mm:ss Example: 2011-05-19 13:51:21 (default Unix format ‘F j, Y’)

post_content
(string) The post content.

post_title
(string) The post’s title.

post_excerpt
(string) The post’s excerpt, if one is set.

post_name
(string) The post’s slug.

post_modified
(string) Date the post was last modified using server’s timezone.

post_parent
(integer) ID of the post’s parent, if it has one.

post_type
(string) The post type.

comment_count
(integer) The number of approved comments.

You can easily get  the value , see the next example:

<?php
global $post;
echo $post->post_title;
?>

Here, echo prints out the post_title.

The next line, get the page permalink  using the function get_permalink pointing to $post->ID  .

Then format the permalink using js_escape and stores the result in the var $link

$link= js_escape(get_permalink($post->ID));

The next line get the post content and stores it in the variable $content

 $content=$post ->post_content;

The next and long line , calls the facebook API.  and adds our page link using the variable $link.

The facebook code adds the button and the formatting.

Then everything is stored in the variable $button

$button = '<iframe src="http://www.facebook.com/plugins/like.php?href='.$link.'&amp;layout=button_count&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp" style="overflow:hidden;width:100%;height:80px;" scrolling="no" frameborder="0" allowTransparency="true"></iframe>';

Finally , we return the button and the post content. Button comes first.

return $button.$content ;

But this function will not work until we use a hook

We must add the next line to tell wordpress to apply the function to every post.

add_filter('the_content',WPFacebook_button);

When activate the plugin, in every post we shall see the like button:

facebook_like_button_screenshot

 

Written by Ariel Campos

You may also like...