Last active
December 12, 2015 08:18
-
-
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.
This file contains hidden or 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 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 | |
| ?> |
This file contains hidden or 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
| 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