Skip to content

Instantly share code, notes, and snippets.

@mojowen
Last active December 12, 2015 08:18
Show Gist options
  • Select an option

  • Save mojowen/4743131 to your computer and use it in GitHub Desktop.

Select an option

Save mojowen/4743131 to your computer and use it in GitHub Desktop.
Adding custom javascript to a WordPress Theme. These would both go in your theme or child-theme's directory.
<?php
function add_my_script() {
wp_enqueue_script(
'my-script', // The name of your script
get_stylesheet_directory_uri().'/my-script.js', // Your script
array('jquery') // Dependencies - so it knows to put your script AFTER jQuery
);
}
add_action('wp_enqueue_scripts', 'add_my_script'); // The action that will add your script
?>
jQuery(document).ready(function($) { // You need to use jQuery not $ as WordPress runs jQuery in no-conflict mode (so $ doesn't work)
// $() will work in here as alias for jQuery() inside of this function
confirm('oh crap I am in the dom and executing....... yes?');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment