Skip to content
Advertisement

JS files doesn’t work correctly in wordpress

As I’ve mentioned in the title, I’m having some problems with my js files.
I’ve tested the scripts in the HTML template and works fine, but when I put them into the functions.php file there are some scripts that doesn’t work as they should.

Here the functions.php file:

<?php
function load_stylesheets(){
 /************/
}

function add_js(){
    wp_register_script('jquery-min', get_template_directory_uri().'/js/jquery.min.js', array('jquery'), 2, true);
    wp_enqueue_script('jquery-min');
    wp_register_script('jquery-migrate', get_template_directory_uri().'/js/jquery/jquery-migrate.min.js', array('jquery'), 2, true);
    wp_enqueue_script('jquery-migrate');
    wp_register_script('popper', get_template_directory_uri().'/js/popper/popper.min.js', array('jquery'), 2, true);
    wp_enqueue_script('popper');
    wp_register_script('bootstrap', get_template_directory_uri().'/js/bootstrap/js/bootstrap.min.js', array('jquery'), 2, true);
    wp_enqueue_script('bootstrap');
    wp_register_script('easing', get_template_directory_uri().'/js/easing/easing.min.js', array('jquery'), 2, true);
    wp_enqueue_script('easing');
    wp_register_script('jquery.waypoints', get_template_directory_uri().'/js/counterup/jquery.waypoints.min.js', array('jquery'), 2, true);
    wp_enqueue_script('jquery.waypoints');
    wp_register_script('jquery.counterup', get_template_directory_uri().'/js/counterup/jquery.counterup.js', array('jquery'), 2, true);
    wp_enqueue_script('jquery.counterup');
    wp_register_script('carousel', get_template_directory_uri().'/js/owlcarousel/owl.carousel.min.js', array('jquery'), 2, true);
    wp_enqueue_script('carousel');
    wp_register_script('lightbox', get_template_directory_uri().'/js/lightbox/js/lightbox.min.js', array('jquery'), 2, true);
    wp_enqueue_script('lightbox');
    wp_register_script('typed', get_template_directory_uri().'/js/typed/typed.min.js', array('jquery'), 2, true);
    wp_enqueue_script('typed');
    wp_register_script('contactform', get_template_directory_uri().'/js/contactform.js', array('jquery'), 3, true);
    wp_enqueue_script('contactform');
    wp_register_script('main', get_template_directory_uri().'/js/main.js', array('jquery'), 3, true);
    wp_enqueue_script('main');
}
add_action("wp_enqueue_scripts", "load_stylesheets", "add_js", 999);
?>

Thanks in advance!

Advertisement

Answer

You cannot hook into wp_enqueue_scripts and call multiple functions like that:

add_action("wp_enqueue_scripts", "load_stylesheets", "add_js", 999);

add_action() accepts these four parameters:

  • $tag (string) (Required) The name of the action to which the $function_to_add is hooked.
  • $function_to_add (callable) (Required) The name of the function you wish to be called.
  • $priority (int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
    Default value: 10
  • $accepted_args (int) (Optional) The number of arguments the function accepts. Default value: 1

So, to add both your stylesheets and scripts you’ll have to do:

add_action( 'wp_enqueue_scripts', 'load_stylesheets', 999 );
add_action( 'wp_enqueue_scripts', 'add_js', 999 );
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement