Last active
September 26, 2022 12:09
-
-
Save rxnlabs/8c73563342d4455cf9ef to your computer and use it in GitHub Desktop.
WordPress - enqueue javascript data based on WordPress conditions and other scripts. Load scripts tags before and after dependent script loads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function load_scripts(){ | |
global $wp_scripts; | |
wp_register_script( 'theme-scripts', get_bloginfo('template_url').'/js/scripts.js', array('jquery'), '1.0', true ); | |
//if we're on the woocommerce checkout page | |
if( is_checkout() ){ | |
$wp_scripts->add_data('theme-scripts','data','<!--START SCRIPT STRING--> | |
// script to load before name-of-enqueued-script loads | |
jQuery(\'input#billing_first_name\').appear(function(){ | |
jQuery(this).focus(); | |
}); | |
<!--END SCRIPT STRING-->'); | |
} | |
} | |
add_action('wp_enqueue_scripts','load_scripts' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function load_theme_scripts(){ | |
wp_register_script( 'name-of-enqueued-script', get_bloginfo('template_url').'/js/scripts.js', array('jquery'), '1.0', false ); | |
} | |
add_action('wp_enqueue_scripts','load_theme_scripts' ); | |
/ http://wordpress.stackexchange.com/questions/33008/how-to-add-a-javascript-snippet-to-the-footer-that-requires-jquery | |
function load_this_other_script(){ | |
if( wp_script_is( 'name-of-enqueued-script', 'done' ) ) { | |
?> | |
<script type="text/javascript"> | |
// script to load after name-of-enqueued-script loads | |
</script> | |
<?php | |
} | |
} | |
add_action( 'wp_head', 'load_this_other_script' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment