Ignoring the potential to do this with jQuery one thing you could do is hook the template_include
filter and use ob_start()
with a callback. Your callback can then do a string search on '<body'
and replace it with '<body onload="init()"'
like the following code does. You should be able to drop it directly in your plugin, just be sure to change the names to follow your plugin's own naming convention:
<?php
add_filter('template_include','start_buffer_capture',1);
function start_buffer_capture($template) {
ob_start('end_buffer_capture'); // Start Page Buffer
return $template;
}
function end_buffer_capture($buffer) {
return str_replace('<body','<body onload="init()"',$buffer);
}
Note that I would not assume the above code to be fully robust yet if I were you. I doubt it will handle all edge cases since I just threw it together to answer your question but minimally it shows you how to accomplish the normal case, and then with some use-case testing I'm sure you'' get it to handle all the important edge cases (like what if '<BODY'
is uppercase, etc?)