-1

I am developing a child theme based on the roots theme. I created a front-page.php and added the following. I test that the page is loaded because I see the I am loaded text. However, the scripts are not loaded. What is wrong with my setup?

<?php
function dd_register_scripts() {
    wp_register_script(
        'something',
        get_template_directory_uri() . '/assets/js/something.min.js', array('jquery'), null, false
    );
}


function dd_enqueue_scripts() { 
    wp_enqueue_script('something'); 
}

add_action('init', 'dd_register_scripts');
add_action('wp_enqueue_scripts', 'dd_enqueue_scripts');
?>
<div id="asd">I am loaded</div>

1 Answer 1

0

Based on your description that you "created a front-page.php and added the following" and also that there is markup in that file, you are hooking too late.

You should be both registering and enqueueing on wp_enqueue_scripts but that isn't the problem here. The problem is that by the time you try to hook, the hooks have already fired. init will fire before your templates load at all and wp_enqueue_scripts will fire in the page header, which is before this code runs (or you have very broken markup). You can't hook to a hook after it has fired.

You need to add this code to the theme's functions.php with the appropriate template tags to conditionally load the scripts.

Related:

https://wordpress.stackexchange.com/a/105135/21376

0

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.