• Ive looked at youtube videos and the code would work in js.fiddle but when I put it into my wordpress post and pages to troubleshoot, they just don’t work.

    Some tutorials say to call the jquery script from https://code.jquery.com/jquery-2.1.3.min.js, some say you can just paste it unto the page, some say to enqueque it.

    I’m not quite sure which tutorial to follow when it comes to learning to use javascript/jquery with wordpress.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Using JavaScrupt you should use wp_enqueue_script() to enqueue the script file. That way you can make sure that you set ‘jquery’ as a dependancy, and WordPress will load the jQuery file for you so you don’t have to do anything about that yourself.

    As a basic idea… This is all you need to add to your file to make it happen

    add_action( 'wp_enqueue_scripts', 'my_enqueue_function' );
    
    function my_enqueue_function() {
        wp_enqueue_script ('my-script', '/path/to/file.js', array ( 'jquery' ));
    }

    As long as your JavaScript file is right then it will work correctly.

    Thread Starter clouder

    (@clouder)

    OH I see, but whenever I try to trouble shoot files in post or pages (where hey say Javascript is enabled) it doesn’t work so I’m not sure which one would actually work

    So for example i try to put something as basic as

    <script type="text/javascript">
    function poodle() {
    alert("Hey");
    }
    
    poodle();
    </script>

    it won’t work so I’m not sure which code would work on javascript before I start enqueue them.

    So I guess my question would be where could I trouble shoot my javascript.

    From what you’ve got there, you should get the alert to work. There’s nothing wrong with that code that would stop anything happening.

    Do you have a URL that we can see to see where the error is and what’s happening?

    Have you tried looking at the console to see if there are any javascript errors?

    Using Google Chrome, simply right-click anywhere on the page and then click “inspect element”. Then click on the Console tab.

    Let us know if it says anything.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trying to learn Jquery for WordPress but none of the tutorials work with WP’ is closed to new replies.