I am currently working with jquery and wordpress plugin.

jQuery(document).ready(function($) {
          alert("hello world");

This code working very well when run on the site as a usual visitor(non logged in user), but it is refusing to work when I am logged in on that wordpress site.

Is there anything wrong in my code that could possibly make this problem happen?

After try to open my consol, I try to enter my code above and run it via console. and then try to click the button again, the code working. I dont understand why since my js file is there, included and printed in wp_footer() like this

<script type="text/javascript" src="....url..../mycode.js"></script>

that mean --if I'm not wrong -- my code is there, but the function

jQuery(document).ready(function($) {

not executed when document is ready. That is why when I try to run it via console, the program run just like when I am not logged in.

Any suggestions

  • Is there a chance you are doing an ajax call? if yes then you need to use both the hooks. check wordpress.org for more details. – Maruti Mohanty Oct 14 '14 at 9:58
  • jQuery(document).ready() fails when a script is loaded async. The better approach to add a click handler is jQuery.on('click','.btn',function(event){ /* do stuff */ }). – Jörn Lund Oct 14 '14 at 9:59
  • @marutiMohanty> no, the script is just as simple as i posted. i am not doing any ajax call.. – user3791830 Oct 14 '14 at 10:05
  • 1
    flagging it to close as off-topic as it is not obvious how wwordpress might part of the problem here. What you need to do is to find the differences between the pages/js generated for logged-in and non logged-in user and edit your question with that info. Without it even if the question is on topic it is very hard to guess why you get such a behaviour. – Mark Kaplun Oct 14 '14 at 10:33
  • 1
    Can you provide live example? There us nothing in code you quoted which would cause such behavior. – Rarst Oct 14 '14 at 13:50

i finally know why this code not work. as menstioned by Jorn Lund in the comment that


will fail when script loaded async ( i still dont know is my code load async) but with in mind i change from


to self execution function


and the code work very well on both loggedin and non loggedin user.

| improve this answer | |

Your Answer

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

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