if ( ! function_exists( 'loadingscripts' ) ) {
function loadingscripts() {
        // Register the script like this for a theme:
        wp_register_script( 'custom-js', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ), '1.1', true );
        wp_enqueue_script( 'custom-js' );

        /* Load the stylesheets. */
        // wp_enqueue_style( 'fontawesome', THEMEROOT . '/inc/lib/fontawesome/css/font-awesome.min.css' );
        wp_enqueue_style( 'layout', THEMEROOT . '/css/layout.css' );
        wp_enqueue_style( 'styles', THEMEROOT . '/css/style.css' );

I think the Javascript is loading before the DOM because in its pure HTML version I was loading the javascript in the footer, but in the WordPress conversion, it doesn't work. What is the fix?

  • What exactly is your question? Is the file (custom,js) not correctly loaded or is the code in it not executed as you expect? (for the latter, we obviously need at least parts of the code). Are you using jQuery and not $ (which does not work by default in WP)?
    – kero
    Jan 2 '18 at 14:15
  • js is loaded, but I think the JS loads earlier than DOM so menus are not working which are dependent on custom.js. Did u get it? Jan 2 '18 at 14:24
  • 2
    jQuery(document).ready(function(){ /* your code here */ });?
    – kero
    Jan 2 '18 at 14:29

Javascript runs as soon as it's loaded, you need to wait for the DOM ready event, and you do that in JS, not in WP:

    /* your code here */

I had to move my custom scripts to the bottom of my footer by editing the add_action

add_action('wp_enqueue_scripts','loadingscripts', 9999);

The 3rd parameter is the priority that your function loads in. giving it a 9999 value will push it to the bottom. if you know the code works on its own my best guess would be that its a plugin's script file that is not playing nice with your script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.