Include & Register Javascript files in WordPress in functions.php


/ Published in: PHP
Save to your folder(s)

This code should be added to the functions.php file in the theme's folder. The code will deregister the local copy of jQuery and instead include the google hosted copy. It will also register other scripts.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function my_script_init() {
  4. if (!is_admin()) {
  5. // deregister local copy of jQuery
  6. wp_deregister_script('jquery');
  7. // register google hosted jquery
  8. wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', false, '1.6.1');
  9. wp_enqueue_script('jquery');
  10.  
  11. // register some other javascript file (just for the sake example)
  12. wp_register_script('otherfile', get_bloginfo('stylesheet_directory').'/scripts/jquery.otherFile.js', array('jquery'));
  13. wp_enqueue_script('otherfile');
  14.  
  15. // register another file that requires that the other two files are loaded first
  16. wp_register_script('mainjs', get_bloginfo('stylesheet_directory').'/scripts/main.js', array('jquery','otherfile'));
  17. wp_enqueue_script('mainjs');
  18.  
  19. wp_deregister_script( 'l10n' );
  20. }
  21. }
  22. add_action('init', 'my_script_init');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.