wp_enqueue_script does not work

3

I'm creating a WordPress theme, but wp_enqueue_scripts does not work. The code that is in functions.php is:

<?php
function scripts_and_styles() {

  if ( ! is_admin() ) {
      wp_enqueue_script( 'jquery' );
      wp_register_style( 'style.css',get_template_directory_uri().'/style.css' );
      wp_enqueue_style( 'style.css' );
      wp_register_style( 'bootstrap.min.css','https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
      wp_enqueue_style( 'bootstrap.min.css' );
    }
}

add_action('wp_enqueue_scripts', 'scripts_and_styles');
?>

Other information that may help you identify what is happening:

  • When I enter echo 'Oi!'; appears Hi! in index.php , as expected;
  • I have to insert in header.php o <?php do_action('wp_enqueue_scripts') ?> , and from what I understood this would not be necessary;
  • When I use var_dump on wp_enqueue_script( 'jQuery' ); and on wp_enqueue_script() it is 'printed' NULL on index.php ;
  • Nothing that uses jQuery works on my theme (I guess because I can not call it);
  • I use XAMPP .

Just to make it clear: It's not just jQuery that does not work, nothing is included. It does not call neither style nor script. I am aware that jQuery comes included in Wordpress and that I have to point out if any script depends on it.

    
asked by anonymous 12.09.2016 / 14:23

3 answers

2

I solved the problem by adding wp_head() and wp_footer() !

    
12.09.2016 / 17:45
2

jQuery, as well as several other scripts , is already included in core of WordPress, and they work on-demand , that is, only when they are needed.

What happens is that, within the logic of WP, you can create scripts dependent on other scripts without worrying about the order of inclusion. You just make clear the dependency that one script has of the other, and let WP make inclusion in the correct order.

Imagine that you have a script animacoes.js (which is dependent on jQuery), and you want to include it in your page. Since jQuery already exists in WP, you do not have to worry about it (which is the case with the first line of your !is_admin() condition.) What you need to do is tell WP that this dependency exists, and that it does the inclusion , as follows:

 wp_register_script('animacoes', get_template_directory_uri() . '/js/animacoes.min.js', array('jquery'), '1.0', true);

See here I'm just registering the script. I am not making the inclusion itself. The method wp_register_script() gets some parameters, which are:

  • The handler name of your script . (From that point on, it's the name you'll refer to in your code);
  • The script path
  • An array containing the handlers of the dependencies. Notice that the handler of jQuery is jquery , with the letter q in the box. If you have more than one dependency , simply add it to this array ;
  • Your script version
  • A bool to place it or not at the bottom of the page.
  • Once registered, just queue it , using the method wp_enqueue_script() , as follows:

    wp_enqueue_script('animacoes');
    

    Where the parameter is the handler of the script previously registered.

    WordPress is careful to work with jQuery in noConflict , because of other libraries that can use the $ and another series of reasons that is not the case. You may have tried to open the console on your site and run $('html').addClass('classe') (or basically anything else using the $() method), and have ended up getting a $ is not defined . This is precisely because of this. If you run jQuery('html').addClass('classe') , you'll see that everything works out.

    To circumvent this fact, and to use $ with no problem in your script animacoes , you can do something like the following:

    //animacoes.js
    (function($){
    
        $('html').addClass('class');
        // code...
    
    }) (jQuery);
    

    Basically, within this script , you're saying $ should be resolved by jQuery.

    This is the most common way to work with scripts within WP. It may sound kinda strange at first, but you get the hang of it soon enough. Remember to always use what the tool already offers. Working by registering and "unregistering" scripts may end up being highly unproductive.

        
    12.09.2016 / 15:38
    0

    Try to do this:

    wp_deregister_script( 'jquery' ); // remove o registro se houver
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"), false);
    wp_enqueue_script('jquery');
    
        
    12.09.2016 / 15:02