0

I have this jquery code inside my wordpress admin page that checks if the input value is empty.

$j(function(){
$j('form').submit(function(){

     var link = $j('#input').val();
    if(link == ''){
         alert('hi there');
    }

});

});

Now if the form is empty it will alert ' hi there'.

the plugin works fine but the alert does not show on the main page. Only in the admin section.

what would be the problem?

here is how I am calling jquery.

function test(){
wp_enqueue_script('jquery');
wp_enqueue_script('myscript', plugin_dir_url(__FILE__). 'js/test.js');


}
add_action('wp_enqueue_scripts','test');
add_action('admin_head','test');

I would like that if the value of input is empty, the alert show also in the wordpress homepage and not only in the admin page.

2
  • The first bit of javascript is where exactly? In your test.js file?
    – Wyck
    Jun 21, 2012 at 16:05
  • yes Wyck thats test.js
    – Ronny K
    Jun 21, 2012 at 16:33

2 Answers 2

1
function test(){
    wp_enqueue_script('myscript', plugin_dir_url(__FILE__). 'js/test.js',array('jquery'));
}
add_action('admin_print_scripts','test');
add_action('wp_head','test');
4
  • added that, but same thing Gembel. the alert only shows at the admin page.
    – Ronny K
    Jun 21, 2012 at 16:39
  • hmm.. not sure why. is your js appear when you view page source? just in case you forget to add wp_head(); in your template :D
    – Dazilio
    Jun 21, 2012 at 16:48
  • yes it does appear Gembel.
    – Ronny K
    Jun 21, 2012 at 16:57
  • how about testing your js in jsbin? here is my test jsbin.com/omumit/edit#javascript,html
    – Dazilio
    Jun 21, 2012 at 17:54
-1

I believe the problem is that you are enquequeing jquery.

You don't need to do that, as WordPress does it by default. Just remove

wp_enqueue_script('jquery');

and everything should be fine.

4
  • that will never work. this is a plugin and I am calling jquery.
    – Ronny K
    Jun 21, 2012 at 16:32
  • But you do not need to call jQuery! Trust me, I have recently submited plugin to WordPress.org and before approving it, I needed to remove jQuery call. There is no need for calling jQuery with plugins! Jun 21, 2012 at 16:35
  • ok well... gave that a shot too.. no changes.
    – Ronny K
    Jun 21, 2012 at 16:40
  • What does your browser console say? Jun 21, 2012 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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