Add javascript to worddpress

Asked

Viewed 44 times

0

I have an application that uses wordpress and I need to add a javascript in it, I saw several videos and posts on the internet and I understood just do this:

<?php
    wp_register_script("meuscript", "js/meuscript.js");

    function my_scripts_loader() {
        wp_enqueue_script("meuscript", "js/meuscript.js");
    }
    add_action("wp_enqueue_scripts", "my_scripts_loader");
?>

But I don’t know where to add this code I got a little confused, I saw something about adding to the theme or something

2 answers

1

Dude, depending on how is configured your theme is just put the path normally in the theme header that works.

<script src="caminhoDoSeuCodigo/code.js"></script>

I’ve done so, I don’t understand much of Wordpress but it worked in my case.

  • It’s the simplest way, I prefer it that way too, you just have to be careful if you’re changing the subject.

  • Yes, I found it easier that way.

  • This way of doing it is not "wrong"?

  • @Guilhermecostamilam if it worked ta right hehehe So as I said do not understand much of wordpress I believe that has somewhere to put that is more suitable not to give dick when changing theme.

0


You can put this code in the file wp-content/themes/SEU-TEMA/functions.php

function load_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/myscript.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'load_scripts' );
  • I put exactly the code up this way but it didn’t work

  • Thus puts https://pastebin.com/9zNCbXQ1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.