J
J
Jokerrs5672021-10-20 11:44:10
JavaScript
Jokerrs567, 2021-10-20 11:44:10

How to include script via functions.php on wordpress?

<script type="2ef1d8b03fb0311f9e134e45-text/javascript">$('.selectpicker').selectpicker();</script>

Who can show an example of the correct connection of the script through functions.php?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-10-20
@Jokerrs567

You can make a dependency on the main script withwp_add_inline_script()

add_action('wp_enqueue_scripts', function() {

  wp_enqueue_script( 'my_scripts', get_stylesheet_directory_uri() .'/my_scripts.js' );
  wp_add_inline_script( 'my_scripts', '$(\'.selectpicker\').selectpicker();' );
  
});

/* Получим в HTML коде
<script type='text/javascript' src='http://example.com/wp-content/themes/mytheme/my_scripts.js'></script>
<script type='text/javascript'>
$('.selectpicker').selectpicker();
</script>
*/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question