Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: why is the init hook invoked multiple times?

Tags:

wordpress

I am at a real loss at this problem. I just created a plug-in, and to log the actions in the plug-in (as I can't see the output of the processing), I added a hook to init like so:

add_action('init', 'test_hook');

function test_hook()
{

   global $wpdb;
   $message = "Some informational message here";
   $wpdb->insert("pq_logs", array("message"=>$message), array("%s"));

}

What's bringing me into submission is that instead of getting one line of entry per page load, I get from 1 to 8 row entries of the same log, with different time stamps. The number of entries that is entered is inconsistent and seems to be totally in random (sometimes I'm thinking it depends on the current weather conditions or the stock exchange index).

I did the standard checks, turned off all other plug-ins, but to no avail. I searched for the code for anything that does do_action('init'), but I was only pointed out to one call at wp-settings.php.

Have you also encountered? What did you do?

like image 642
Ardee Aram Avatar asked Dec 07 '10 01:12

Ardee Aram


2 Answers

Do you have any links in the site (images, scripts, stylesheets, etc) that are 404'ing? If so, the default 404 page will display a "You are looking for something that is not here" page in the default site theme, so you'll end up getting the init hook called once for the initial page request, plus once for each bad link.

Doublecheck with Firebug and see if anything is being called incorrectly.

like image 108
Rob Williams Avatar answered Sep 18 '22 06:09

Rob Williams


After Rob Williams' answer I've read this post:

http://www.tastyplacement.com/how-to-remove-link-relprev-and-link-relnext-from-wordpress-head

And realized that the link tags with rel="next" and rel="prev" was running my init hook for a second time on my WordPress 3.5.1 website. These tags can be removed adding this to the theme functions.php file:

remove_filter('wp_head','adjacent_posts_rel_link_wp_head',10);
like image 33
Joan Botella Avatar answered Sep 22 '22 06:09

Joan Botella