Web Design Blog

June 4, 2012

I recently added the Facebook comments and Addthis plugins to my site. One of the issues that occurred was a duplicate ‘Like’ button when the comments plugin loaded. After a quick look at the comments plugin code, I saw that the Facebook javascript SDK was being loaded in the footer.

add_action('wp_footer', 'fbmlsetup', 100);

My first instinct was to move this to the ‘wp_head’ hook, then I realized that wouldn’t be good because it would place the <div> and script inside the <head> element of the page. My experience told me this code is supposed to live right after the <body> tag. Apparently there is no such hook built into WordPress, but to create one in your theme is simple and painless.

In your theme’s functions.php add the following function named ‘after_body':

function after_body(){
	do_action('after_body');
}

In your theme’s header.php call the ‘after_body’ function right after the <body> tag:

<body>
<?php after_body(); ?>

Now just a little hack to the plugin code, I commented out the ‘wp_footer’ hook and replaced it with the ‘after_body’ hook.

// moving this to head to avoid conflict with addThis buttons ... 
// add_action('wp_footer', 'fbmlsetup', 100);
add_action('after_body', 'fbmlsetup', 100);

This resolved the issue and helped me understand how simple it is to create a custom action hook in WordPress.

Comments

comments

Powered by Facebook Comments