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.

In my functions.php

    function load_scripts() 
        {
 wp_enqueue_script( 'jquery' );
wp_register_script( 'modernizr', get_template_directory_uri() .'/js/modernizr.foundation.js' );
wp_enqueue_script( 'modernizr' );
wp_register_script( 'jquery.simpleWeather', get_template_directory_uri() .'/js/jquery.simpleWeather.js', array( 'jquery'));
wp_enqueue_script( 'jquery.simpleWeather' );
        }
        add_action( 'wp_enqueue_scripts', 'load_scripts ');

I try to register and load scripts to my wp theme. For jquery.simpleWeather.js i have a part of code that load in the header.php

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>

<script type="text/javascript">
$.simpleWeather({
zipcode: '',
woeid: '',
location: '',
unit: 'f',
success: function(weather) {
html = '<span>'+weather.city+', '+weather.region+'</span>';
html += '<img style="float:left;" width="125px" src="'+weather.image+'">';
html += '<p>'+weather.temp+'&deg; '+weather.units.temp+'<br /></p>';
$(".weather").html(html);
},
error: function(error) {
$(".weather").html('<p>'+error+'</p>');
}
}); 
</script>

Also i edit the code :

<script type="text/javascript">
 jQuery(document).ready(function($) {
$.simpleWeather({    
zipcode: '',    
woeid: '',   
location: '',   
unit: 'f',   
success: function(weather) {
html = '<span>'+weather.city+', '+weather.region+'</span>';
html += '<img style="float:left;" width="125px" src="'+weather.image+'">';
html += '<p>'+weather.temp+'&deg; '+weather.units.temp+'<br /></p>';
$(".weather").html(html);    
},
error: function(error) {    
$(".weather").html('<p>'+error+'</p>');
} 
});    
</script>

But the scripts didn't register and load. Any idea?

Thanks.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

You don't need if ( !is_admin() ). wp_enqueue_scripts only runs on the front end.

WordPress loads jQuery in "No Conflict" mode. The $ alias does not work. Use jQuery or wrap your script is a function as demonstrated in the jQuery docs:

jQuery.noConflict();
(function($){
  $(function() { 
   // more code using $ as alias to jQuery
  });
})(jQuery);

You claim that your script loads after jQuery but I don't see that it loads at all, and certainly not after jQuery.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<!-- missing jquery.simpleWeather.js -->
<script type="text/javascript">
$.simpleWeather({

Your jquery.simpleWeather.js has to load at the line marked in the code or your attempt to use it in the following line will fail, but there is no sign of that script. If it has loaded, it loaded before jQuery, which will also fail since it is a jQuery plugin and is dependent upon jQuery.

Also, I get an 'invalid callback' error with your code. I don't know why. It looks fine. Possibly there is some oddball character in the code somewhere. I had to completely rewrite the function to get it working. If you can't get this working copy the following code.

function load_my_scripts() {
  wp_enqueue_script( 'jquery' );
  wp_register_script( 'modernizr', get_template_directory_uri() .'/js/modernizr.foundation.js' );
  wp_enqueue_script( 'modernizr' );
  wp_register_script( 'jquery.simpleWeather', get_template_directory_uri() .'/js/jquery.simpleWeather.js', array( 'jquery'));
  wp_enqueue_script( 'jquery.simpleWeather' );
}
add_action('wp_enqueue_scripts','load_my_scripts');

Somewhat off-topic but what software are you using to edit this code?

share|improve this answer
    
You don't need the jQuery.noConflict()-- that is directly from the jQuery docs. Is your script loading after both jQuery and simpleWeather? –  s_ha_dum Feb 12 '13 at 21:54
    
yes the script load after jQuery. –  MikeD Feb 12 '13 at 21:56
    
Any solution? i am try to edit and add the code, but the result is the same. I know that the choice that i have made, maybe is not the best, but if i leave it as it is, I will have some delay in disengagement or code? When i refresh the page (local environment) i notice that the weather.js has delay. Thanks again –  MikeD Feb 13 '13 at 23:19
1  
Where is your opening <script> tag? Please edit your question with new information. Code is nearly unreadable in the comments. –  s_ha_dum Feb 13 '13 at 23:23
    
thanks, i edit my question with the new information. –  MikeD Feb 13 '13 at 23:58
add comment
header.php

....

<?php wp_enqueue_script('jquery'); ?> 
.....
<script type="text/javascript">
$(document).ready(function() {
$.simpleWeather({
zipcode: '',
woeid: '',
location: '',
unit: '',
success: function(weather) {
html = '<h6>'+weather.city+', '+weather.region+'</h6>';
html += '<img style="float:left;" width="125px" src="'+weather.image+'">';
html += '<p>'+weather.temp+'&deg; '+weather.units.temp+'<br /></p>';
$(".weather").html(html);
},
error: function(error) {
$(".weather").html('<p>'+error+'</p>');
}
}); 
});
</script>

in the functions.php

function load_my_scripts() {
wp_enqueue_script( 'jquery' );
wp_register_script( 'modernizr', get_template_directory_uri() .'/js/modernizr.foundation.js' );
wp_enqueue_script( 'modernizr' );
wp_register_script( 'jquery.simpleWeather', get_template_directory_uri() .'/js/jquery.simpleWeather.js', array( 'jquery'));
wp_enqueue_script( 'jquery.simpleWeather' );}
add_action('wp_enqueue_scripts','load_my_scripts');?>

and works, Thanks for the feedback.

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.