• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

I cannot get wp_enqueue_scripts to load a JS file

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › General Genesis Framework Discussions › I cannot get wp_enqueue_scripts to load a JS file

This topic is: not resolved

Tagged: jquery, wp_enqueue_script

  • This topic has 6 replies, 2 voices, and was last updated 9 months, 2 weeks ago by gillespieza.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • April 14, 2022 at 5:25 am #505025
    gillespieza
    Participant

    I am trying to load a javascript file into my theme. I'm using the genesis-sample theme, with Genesis 3.3.5 as the parent.

    I can successfully load the following CSS:

    
    function pathway_load_bootstrap_css() {
    
    	wp_enqueue_style(
    		genesis_get_theme_handle() . '-bootstrap',
    		get_stylesheet_directory_uri() . '/lib/pathway/css/bootstrap.min.css',
    		null,
    		'5.1.0',
    		'screen'
    	);
    
    }
    add_action( 'wp_enqueue_scripts', 'pathway_load_bootstrap_css' );
    

    And when I view the source, I can see the CSS file is loaded in the header. Great, perfect. However, when I try to do the same with a javascript file, nothing happens. I don't get any errors, but the file is not there when I view source:

    
    function pathway_load_bootstrap_js() {
    
    	wp_register_script(
    		genesis_get_theme_handle() . '-bs-bundle',
    		get_stylesheet_directory_uri() . '/lib/pathway/js/bootstrap.min.js',
    		array( 'jquery' ),
    		'5.1.0',
    		true
    	);
    
    }
    add_action( 'wp_enqueue_scripts', 'pathway_load_bootstrap_js' );
    

    What have I done wrong here? I'm wracking my brains trying to figure this out. I've even disabled all plugins (no change), and tried changing the hook from wp_enqueue_scripts to genesis_after_footer, all with no change.

    https://staging.mypathwayjourney.com/
    April 14, 2022 at 5:33 am #505026
    Brad Dalton
    Participant

    Replace wp_register_script with wp_enqueue_script and clear caching.


    Get Help – Book Consultation.

    April 14, 2022 at 5:41 am #505027
    gillespieza
    Participant

    OMG, it worked! What??? Why though? I have always registered my scripts first, and then enqueued. I know you can do it all in one step, but anyhoo. Why does the registering and then enqueuing not work? I checked in my debug log, and wp_register_script is definitely returning true, so ... I don't understand what's happening anymore 🙁

    April 14, 2022 at 5:51 am #505028
    Brad Dalton
    Participant

    Where's your code to enqueue?


    Get Help – Book Consultation.

    April 14, 2022 at 5:57 am #505029
    Brad Dalton
    Participant

    See here https://developer.wordpress.org/reference/functions/wp_register_script/#description

    wp_register_script Registers a script to be enqueued later using the wp_enqueue_script() function.


    Get Help – Book Consultation.

    April 14, 2022 at 6:05 am #505030
    gillespieza
    Participant

    Yeah, that's exactly what I followed. My code is in my functions file.

    Oh god, I see it now. Doh!

    April 14, 2022 at 6:16 am #505031
    gillespieza
    Participant

    For reference, if anyone else is having the same problem, it was clearly a PEBKAU error.

    I was registering the script correctly with wp_register_script but had not enqueued it with wp_enqueue_script, before hooking into the wp_enqueue_scripts action.

  • Author
    Posts
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Log In

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble