Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rxnlabs/8c73563342d4455cf9ef to your computer and use it in GitHub Desktop.
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
<?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' );
<?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