0

I'm writing a simple wordpress theme. I've added this function inside the functions.php file and I've noticed that only style.css is loaded. Is there something wrong that will cause that the other scripts aren't loaded?

function load_theme_assets()
{
    //wp_register_style( $handle:string, $src:string|false, $deps:array, $ver:string|boolean|null, $media:string )
    wp_register_style(
        'main',
        get_template_directory_uri() . '/style.css',
        false
    );
    wp_enqueue_style('main');

    //wp_register_script( $handle:string, $src:string|false, $deps:array, $ver:string|boolean|null, $in_footer:boolean )
    wp_register_script(
        'main',
        get_template_directory_uri() . '/index.js',
        [],
        false,
    );
    wp_enqueue_script('main');

    wp_register_style(
        'bootstrap',
        'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css',
        false
    );
    wp_enqueue_style('bootstrap');
}
add_action('wp_enqueue_scripts', 'load_theme_assets');

?>
5
  • 1
    Is the script not loading or is it not working? It’s an important difference. Do you see the script tags in the HTML or not. Commented Mar 17, 2023 at 11:40
  • do you see failed loading errors on the frontend? How are you testing that the scripts are failing to load? Note that loading Bootstrap from a CDN is not more performant unless you're using a 10 year old browser and can actually be much slower ( modern browsers keep separate caches for each pages domain so there's no shared cache for CDNs ), and there are regulatory consequences too, this may not be GDPR compatible if we go by recent Google Fonts rulings Commented Mar 17, 2023 at 12:53
  • no, the script will not load, no error is logged in console. This is what is strange. In my index.php I've included the get_header() and get_footer() functions and only the style.css is loaded Commented Mar 17, 2023 at 13:17
  • But have you used wp_head() and wp_footer() inside of the header and footer? Commented Mar 17, 2023 at 13:53
  • 1
    @ICTDEV, how did you confirm this - "the script will not load" - are you sure the raw/server-generated HTML source does not contain a script tag for your script? Are you sure the script is at get_template_directory_uri() . '/index.js' (i.e. the path is correct and that it exists)? Commented Mar 17, 2023 at 16:23

1 Answer 1

0

I believe your mistake is using main as handle keyword for both script and style, handle must be unique. You don't have to use wp_register_script (and style) enqueue function does this automatically. You typically use wp_register_script(style) only if you are not sure if script/style will be needed later, if it is enqueued conditionally, but you want to avoid having several versions of the same script/style loaded. You should check browsers console, you might as well specified incorrect path to those scripts. You can also use newer get_theme_file_uri() function to find path for scripts/styles which has some advantages (see manual).

2
  • I've read on a post that the wp_register_style wp_register_script functions can be used in combination with the wp_enqueue_script wp_enqueue_style functions. Probably my mistake is to use the same id for both scripts, I will test by changing the handle. Thanks for the clarification Commented Mar 20, 2023 at 10:44
  • They CAN be used, but I explained why this is often not necessary Commented Mar 20, 2023 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.