• Resolved bithons

    (@bithons)


    Hi,

    I am trying to add my custom javascript with this code in my functions.php from my child-theme.

    function my_custom_scripts() {
    	wp_register_script( 'custom.js', get_stylesheet_directory_uri() . '../js/custom.js', array( 'jquery' ) );
    	wp_enqueue_script('custom.js');
    }
    
    add_action('wp_enqueue_scripts', 'my_custom_scripts');

    It is loading my script, because I can see my console.log messages in the console, but it is not loading the jquery dependency. Why is this happening?

    I hope you guys can help me out with this weird problem.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter bithons

    (@bithons)

    I fixed the problem I will explain it later, I set the topic status on resolved so people don’t take the time for a problem that is already solved.

    I will edit this message with the explanation.

    Thread Starter bithons

    (@bithons)

    The solution was wrapping my custom javascript code in in:

    (function($){
        // your code here.
    })(jQuery);

    For more information see: Click here

    And for more information about proper way loading the Javascript see the this article what helped me with a clear explanation: Click here

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Adding custom javascript’ is closed to new replies.