Title: Javascript not working in functions.php
Last modified: August 20, 2016

---

# Javascript not working in functions.php

 *  Resolved [edwardgyngell](https://wordpress.org/support/users/edwardgyngell/)
 * (@edwardgyngell)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/)
 * Hello.
 * I am trying to get masonry working on my website but the following code doesn’t
   seem to be calling the javascript into my footer. Can anyone tell me why not.
 * Thanks
 *     ```
       function portfolio_masonry() {
       	if (!is_admin()) {
       		wp_register_script('jquery_masonry', get_template_directory_uri(). '/js/jquery.masonry.min.js', array('jquery'), '2.0.110526');
       		wp_enqueue_script('jquery_masonry');
       		add_action('wp_footer', 'portfolio_add_masonry');
   
       		function portfolio_add_masonry() { ?>
       			<script>
       				jQuery(document).ready(function($){
       					$('#masonry-index').masonry({
       						itemSelector: '.index-post-container',
       						isAnimated: true,
   
       					});
       				});
       			</script>
       		<?php
       		}
       	}
       }
   
       add_action('init', 'portfolio_masonry');
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [David Gwyer](https://wordpress.org/support/users/dgwyer/)
 * (@dgwyer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/#post-3041566)
 * Just a hint about enqueueing JS on your front end pages. You can use the ‘wp_enqueue_scripts’
   hook instead of ‘init’ and remove the is_admin() test. Just tidies up the code
   slightly.
 * See here for more details: [http://codex.wordpress.org/Function_Reference/wp_enqueue_script](http://codex.wordpress.org/Function_Reference/wp_enqueue_script)
 * As for the main issue, you have a function inside a function. Try the code below.
   Should work correctly:
 *     ```
       function portfolio_masonry() {
       	wp_register_script( 'jquery_masonry', get_template_directory_uri(). '/js/jquery.masonry.min.js', array('jquery'), '2.0.110526' );
       	wp_enqueue_script( 'jquery_masonry' );
       }
       add_action( 'wp_enqueue_scripts', 'portfolio_masonry' );
   
       function portfolio_add_masonry() { ?>
       	<script>
       		jQuery(document).ready(function($){
       			$('#masonry-index').masonry({
       				itemSelector: '.index-post-container',
       				isAnimated: true,
   
       			});
       		});
       	</script>
       <?php
       }
       add_action( 'wp_footer', 'portfolio_add_masonry' );
       ```
   
 *  Thread Starter [edwardgyngell](https://wordpress.org/support/users/edwardgyngell/)
 * (@edwardgyngell)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/#post-3041585)
 * Thanks for the reply.
 * The masonry.min.js is being called in the header but the javascript isn’t displaying
   in the footer? Will the wp-footer place the script inbetween the <footer></footer
   > tags?
 *  [David Gwyer](https://wordpress.org/support/users/dgwyer/)
 * (@dgwyer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/#post-3041586)
 * Can you post a link please?
 *  Thread Starter [edwardgyngell](https://wordpress.org/support/users/edwardgyngell/)
 * (@edwardgyngell)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/#post-3041593)
 * edwardgyngell.com
 *  [David Gwyer](https://wordpress.org/support/users/dgwyer/)
 * (@dgwyer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/#post-3041595)
 * Make sure your theme implements the wp_footer() function or calling the ‘wp_footer’
   hook won’t do anything. See here:
 * [http://codex.wordpress.org/Function_Reference/wp_footer](http://codex.wordpress.org/Function_Reference/wp_footer)
 * As your jQuery is only running after the document is loaded you could always 
   add it to the ‘wp_head’ hook instead.
 * Otherwise why not just add it to a .js file and enqueue it with a loading dependency
   on the masonry script? Then you wouldn’t have to add the jQuery code manually
   to the header/footer via hooks.
 *  Thread Starter [edwardgyngell](https://wordpress.org/support/users/edwardgyngell/)
 * (@edwardgyngell)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/#post-3041605)
 * I had the hook inserted but it doesn’t want to show up. I will try adding it 
   through a enqueue. Thanks for your help.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Javascript not working in functions.php’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [edwardgyngell](https://wordpress.org/support/users/edwardgyngell/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/javascript-not-working-in-functionsphp/#post-3041605)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
