WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code in my plugin main file :

function my_function() {
<script type="text/javascript"> 
    jQuery(function () {    
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

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

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:


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

Your Answer


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.