Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code in my plugin main file :

function my_function() {
?>
<script type="text/javascript"> 
    jQuery(function () {    
        alert('jQuery');
    });
</script>
<?php
}
add_action("wp_footer", "my_function");

My website does have jQuery loaded and operational (lots of features on my page use it and they work). Yet this alert() doesn't work. Why is that ?

share|improve this question
add comment

2 Answers

That snippet 'works' for me.

Is the script being printed at the bottom of the page? More specifically, does your theme call wp_footer. All themes should, but not all themes do call wp_footer which breaks plugins which rely on it.

For the record - you should not print scripts directly to the page. WordPress has a great API for handling JavaScript enqueing and dependency handling. See this codex page.

You may also find this question helpful too.

share|improve this answer
add comment

Because you just defined a function there. You didn't execute it or attached it to some event, to be executed when the event happens.

You probably want:

jQuery(document).ready(function($){
  alert('jQuery');
});

Now this function will run after the document has loaded.

And try to avoid inline scripts. Use wp_enqueue_script whenever possible.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.