I am creating a plugin to add some javascript to Wordpress's theme header and footer.

I'm doing this through the following wordpress hooks:

add action(wp_head,function_name)
add action(wp_footer,function_name)

However my plugin can't add script if the clients theme does not have wp_head() and wp_footer() functions.

Is there any possibility that I can use if-else in my plugin to check to see if the client's theme has wp_head() and wp_footer() functions.

Or is there any possibilty to add my javascript before </head> and </body> tag without using wp_head and wp_footer hooks.

migrated from stackoverflow.com Jul 2 '11 at 12:22

This question came from our site for professional and enthusiast programmers.


Technically to check if theme runs wp_head and wp_footer hooks you can:

  1. hook into shutdown
  2. check that you are in front end
  3. use did_action() to determine if hooks fired
  4. save the result (in option for example) with theme name to access and use later

However in practice I agree with Chip Bennet that these hooks are essential. Trying to code for case when they are not there is simply not practical (same like trying to code for ancient WP version and such).


To determine the existence of these template tag calls, you'd have to read the contents of the current Theme's header.php and footer.php template files (and probably index.php, too, just for good measure, to cover the oddball Themes), and look for the template tag call string.

But that really doesn't get you anywhere, because without the existence of those template tags, you have no way of injecting content into the document head or footer.

Any Theme that doesn't call wp_head() and wp_footer() has far bigger problems, though; they are essential to core functionality at this point.

  • 1
    +1 on 'Any Theme that doesn't call wp_head() and wp_footer() has far bigger problems' – Ryan Gibbons Aug 1 '11 at 14:30

Check this code: https://gist.github.com/378450

Is a complete solution to check usage both of this function. Code prints string, using wp_head and wp_footer actions, then make a self request to obtain code and check this strings. If doesn't exist add report page.


Why not try using:

if(function_exists('wp_head')) { //do something }else{ //something else }
  • Ok you mean i can use this if(function_exist(wp_head)) in my Plugin. its an good idea but if wp_head function not exist how i can add my javasript in theme's header and footer. – Mohummad Abdullah Jul 2 '11 at 6:15
  • That will only return true if the user has a WordPress version older than 1.5 (for wp_footer()) or 1.2 (for wp_head()), as those were the versions in which the two functions were added to WordPress. – Chip Bennett Jul 2 '11 at 12:56
  • wp_head() always exist (depent on wp version). – Marcin Jul 27 '12 at 6:56
  • Now a days it returns true :) – daniyalahmad Oct 7 '15 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy