Community Forums › Forums › General Genesis Framework Discussions › I cannot get wp_enqueue_scripts to load a JS file
Tagged: jquery, wp_enqueue_script
- This topic has 6 replies, 2 voices, and was last updated 3 years, 7 months ago by
gillespieza.
-
AuthorPosts
-
April 14, 2022 at 5:25 am #505025
gillespieza
ParticipantI 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
https://staging.mypathwayjourney.com/wp_enqueue_scriptstogenesis_after_footer, all with no change.April 14, 2022 at 5:33 am #505026Brad Dalton
ParticipantReplace wp_register_script with wp_enqueue_script and clear caching.
April 14, 2022 at 5:41 am #505027gillespieza
ParticipantOMG, 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_scriptis definitely returningtrue, so ... I don't understand what's happening anymore 🙁April 14, 2022 at 5:51 am #505028Brad Dalton
ParticipantApril 14, 2022 at 5:57 am #505029Brad Dalton
ParticipantSee 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.
April 14, 2022 at 6:05 am #505030gillespieza
ParticipantYeah, 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 #505031gillespieza
ParticipantFor reference, if anyone else is having the same problem, it was clearly a PEBKAU error.
I was registering the script correctly with
wp_register_scriptbut had not enqueued it withwp_enqueue_script, before hooking into thewp_enqueue_scriptsaction. -
AuthorPosts
- You must be logged in to reply to this topic.