Skip to content
Browse files

updated JS to load it in the proper way

Making sure that we are using the default WordPress JS
  • Loading branch information...
1 parent f0ff109 commit 8fb2642770d7903b155225591edbb25f58287a1e @curtismchale committed Dec 19, 2011
Showing with 74 additions and 32 deletions.
  1. +74 −23 assets/includes/add-js.php
  2. +0 −9 header.php
View
97 assets/includes/add-js.php
@@ -1,26 +1,77 @@
<?php
- // only need on the frontend of the site (most scripts)
- function jsNotAdmin() {
- if (!is_admin()){
- wp_enqueue_script('jquery');
- wp_enqueue_script('jqueryvalidate', get_template_directory_uri().'/assets/js/jquery.validate.min.js', array('jquery'), '1.0', true);
- }// end if is_admin new scripts go above this
- }
- add_action('init', 'jsNotAdmin');
- // this chunk deals with removing the version number to enhance caching
- function restatement_footer_scripts() {
- if (!is_admin()){
- $js='/assets/js/scripts.js';
- wp_register_script('scripts', get_stylesheet_directory_uri().$js,array('jquery'),filemtime(STYLESHEETPATH.$js),true);
- wp_print_scripts('scripts');
- }// end if is_admin
- }
- add_action('wp_footer', 'restatement_footer_scripts');
- // scripts needed in admin (almost never)
- function jsInAdmin(){
-
- }// end jsInAdmin
-
- add_action('init', 'jsInAdmin');
+/**
+ * Adds scripts to the front end of the site
+ *
+ * Most scripts are only needed on the front end of the site
+ * and the function below makes sure jQuery is ready then adds
+ * the scripts we need for the site
+ *
+ * @uses wp_enqueue_script
+ * @uses wp_register_script
+ * @uses get_template_directory_uri
+ *
+ * @since 2.2
+ */
+function com_js_not_admin() {
+
+ // registering our scripts first
+ wp_register_script('jqueryvalidate', get_template_directory_uri().'/assets/js/jquery.validate.min.js', array('jquery'), '1.0', true);
+ wp_register_script('comthemescripts', get_template_directory_uri().'/assets/js/scripts.js', array('jquery'), '1.0', true);
+
+ // making sure jquery is ready first
+ wp_enqueue_script('jquery');
+
+ // loading the scripts we need for our theme
+ wp_enqueue_script('jqueryvalidate');
+ wp_enqueue_script('comthemescripts');
+
+}
+
+/**
+ * Adds the JS for threaded comments
+ *
+ * Action is added if we're !is_admin and just before the
+ * comment form it loaded. Then the function itself checks
+ * to make sure that threaded comments are in fact
+ * enabled
+ *
+ * @uses wp_enqueue_script
+ *
+ * @since 2.2
+ */
+function com_threaded_comment_js(){
+
+ if( get_option( 'thread_comments' ) ) {
+ wp_enqueue_script( 'comment-reply' );
+ }
+
+}
+
+/**
+ * Now we're adding the actions for the above functions
+ * but only if !is_admin since we don't need to load them
+ * on the admin side of the site.
+ *
+ * We also make sure that we use the proper hooks for adding scripts.
+ * Using comment_form_before makes sure that if comments are just
+ * not included in the theme we don't bother loading the script.
+ *
+ * wp_enqueue_scripts is the proper spot to load all scripts for
+ * the frontend of your site.
+ */
+if( !is_admin() ){
+ add_action( 'wp_enqueue_scripts', 'com_js_not_admin' );
+ add_action( 'comment_form_before', 'com_threaded_comment_js' );
+}
+
+/**
+ * This is where we'd add any scripts we wanted to actually use
+ * in the admin area of the site which is almost never.
+ */
+function com_js_in_admin(){
+
+}// end jsInAdmin
+
+add_action( 'admin_enqueue_scripts', 'com_js_in_admin' );
?>
View
9 header.php
@@ -43,15 +43,6 @@
<meta name="robots" content="noindex, nofollow" />
<?php }?>
- <?php
- /* We add some JavaScript to pages with the comment form
- * to support sites with threaded comments (when in use).
- * TODO enqueue this properly
- */
- if ( is_singular() && get_option( 'thread_comments' ) )
- wp_enqueue_script( 'comment-reply' );
- ?>
-
<?php wp_head(); ?>
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_uri(); ?>" media="screen" />

0 comments on commit 8fb2642

Please sign in to comment.
Something went wrong with that request. Please try again.