K
K
Katya Petrova2018-08-25 17:15:20
JavaScript
Katya Petrova, 2018-08-25 17:15:20

How to include js file in wordpress?

Hello, what am I doing wrong and where is my mistake?
The fact is that if I write in javascript, then the code works, but if I write in jquery, then the code simply does not work.

header.php file:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
  <meta charset="<?php bloginfo("charset") ?>">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Тест</title>
  <link rel="icon" href="img/fa.ico" type="image/x-icon">

  <?php wp_head(); ?>
</head>

<body>
  <header id="mainscroll" class="header">
    <div class="golova">
      шапка
    </div>
  </header>


index.php file:
<?php get_header() ?>

  <div class="telo">
    тело
  </div>
 		
<?php get_footer() ?>


footer.php file:
<footer id="contscroll" class="contact">
    <div class="foot">
      подвал
    </div>
  </footer>

<?php wp_footer(); ?>
</body>
</html>


function.php file:
<?php
  
   add_action("wp_enqueue_scripts", "test_media");

   function test_media(){
   	wp_enqueue_style("test-main", get_stylesheet_uri());

   	wp_enqueue_script('jquery');
   	wp_enqueue_script("test-script-main", get_template_directory_uri() . '/assets/js/script.js', array('jquery'),null,true);
   }

?>


script.js file
jQuery(function(){
    alert("НЕ РАБОТАЕТ");
}


That is, the text in the alert is not displayed, and if I had written it through javascript, then everything would have worked correctly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lamer350, 2018-08-25
@Ange01

This is all because you forgot the parenthesis at the end:

jQuery(function(){
    alert("НЕ РАБОТАЕТ");
});

P
Pavel, 2018-08-25
@PavelMonro

What does it write in the console?
Are the files connected?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question