I wish to run some code on every page request in Wordpress. I made use of the init and shutdown actions, but on a single page it's calling the technique two times in some way. Can there be an alternate or better approach in PHP in Wordpress.

Mike, presuming that by page request you mean the leading some of it (excluding the admin hits) then wp_footer is the greatest spot to add_action() to.

Calling the technique two times can often mean a couple of things (off the top my mind):

  • The page you are viewing fires an AJAX request which runs with the init action as with every other request.
  • The page you are viewing has been rerouted, that experiences the init action too
  • There's a component around the page leading to it to refresh or firing a phone call to that particular page, Expensive objects enjoy that, especially games, Disqus leaving comments wordpress plugin likes to achieve that too.

To debug you could try to output or log the items in the $_REQUEST array to determine why it's being fired two times.


You can just do this within the page.php file of the selected theme. That php code is performed for exhibiting each page. If you wish to execute some code for posts then use single.php.