Loading Javascript in Functions.php

  • Hi,

    I wish to ask for help in respect of loading a custom JS file in the functions.php file. I currently have the following in the functions.php:


    <?php
    function my_theme_enqueue_styles() {

    $parent_style = 'parent-style';
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version'));
    wp_enqueue_style('jquery.mobile-1.4.5.css', get_stylesheet_directory_uri().'/js/lib/jquery.mobile-1.4.5.css');
    }

    function wp_adding_scripts() {
    wp_register_script('app.js', get_stylesheet_directory_uri() .'/js/app.js', array(), '', true);
    wp_enqueue_scripts('app.js');
    }

    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles');
    add_action( 'wp_enqueue_scripts', 'wp_adding_scripts');

    ?>

    This is currently in my app.js file:

    jQuery(document).ready(function(){
    alert('call');
    });

    When i load my website, i’m getting a 504 gateway timeout. The backend loads fine (I.e. plugins, appearance, etc).

    If i remove the reference to the app.js out of the functions file, the website loads fine, but unfortunately no custom javascript is available.

    Please let me know if there is anything further i can provide.

    Thanks
    Jon

    Hello,

    There is a file specifically for enqueueing files.

    Look for the enqeueue.php file located in the inc folder of the theme.

    Go ahead and enqeueue your app.js file there.

    Let me know if you have any issues.

    Hi Hans,

    Ok to clarify, as I’m using a child theme, would it be best if I create an equivalent enqueue.php file, with my script(s) listed in there, with the stylesheet_template_uri()? Or would it need to be placed within the integral_scripts function?

    Appreciate your time
    Kind regards
    Jonathan

    Hi Hans,

    May I ask if there is any further input regarding whether it needs to be in the child theme / or just the parent theme?

    Kind regards
    Jonathan

    I would add it to the Integral parent theme but you can also add it to the Integral child theme if you wish.

    You will need to add it to the functions.php file for the child theme.

    Thank you Hans, appreciate the help

You must be logged in to reply to this topic.