Title: Adding Javascript Functions to Header.php
Last modified: November 8, 2018

---

# Adding Javascript Functions to Header.php

 *  [starwag](https://wordpress.org/support/users/starwag/)
 * (@starwag)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/)
 * I am working on a theme and adding javascript directly to the header. When I 
   add just two functions the pages work beautifully, but when I add all of the 
   functions I need all javascript stops working. Why does it not work with more
   than two functions?
    Here is the header code after I add all the javascript:
 *     ```
       <!doctype html>
       <html <?php language_attributes(); ?> class="no-js">
       	<head>
       		<meta charset="<?php bloginfo('charset'); ?>">
       		<title><?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' :'; } ?> <?php bloginfo('name'); ?></title>
   
       		<link href="//www.google-analytics.com" rel="dns-prefetch">
   
   
       		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
       		<meta name="viewport" content="width=device-width, initial-scale=1.0">
       		<meta name="description" content="<?php bloginfo('description'); ?>">
   
       		<?php wp_head(); ?>
       <?php if( is_category() ) { ?>
       <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css" />
       <?php } ?>
       <?php if( is_front_page() ) { ?>
       <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css" />
       <?php } ?>
       <?php if( is_home() ) { ?>
       <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css" />
       <?php } ?>
       <?php if( is_page() ) { ?>
       <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css" />
       <?php } ?>
       <?php if( is_archive() ) { ?>
       <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css" />
       <?php } ?>
       <?php if( is_single() ) { ?>
       <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/style.css" />
       <?php } ?>
       		<script>
               conditionizr.config({
                   assets: '<?php echo get_template_directory_uri(); ?>',
                   tests: {}
               });
       			function openNav() {
           document.getElementById("mySidenav").style.width = "25vh";
       }
   
       function closeNav() {
           document.getElementById("mySidenav").style.width = "0";
       }
       			function myFunction1() {
           document.getElementById("extra").style.display = "block";
       }
       function myFunction2() {
           document.getElementById("extra2").style.display = "block";
       }
       function myFunction3() {
           document.getElementById("extra3").style.display = "block";
       }
       function myFunctionC() {
       	document.getElementById("extra").style.display = "none";
       }
       function myFunctionC2() {
       	document.getElementById("extra2").style.display = "none";
       }
       function myFunctionC3() {
       	document.getElementById("extra3").style.display = "none";
       }
   
       		</script>
       	</head>
       	<body <?php body_class(); ?>>
       ```
   

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

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/#post-10860836)
 * see [https://developer.wordpress.org/reference/functions/wp_enqueue_script/](https://developer.wordpress.org/reference/functions/wp_enqueue_script/)
   for the proper way to add scripts to the header.
 *  Thread Starter [starwag](https://wordpress.org/support/users/starwag/)
 * (@starwag)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/#post-10860842)
 * I’ve read that, but I also have read that I can just insert this into the header.
   Is that not true?
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/#post-10860856)
 * You can do that, too.
 *  Thread Starter [starwag](https://wordpress.org/support/users/starwag/)
 * (@starwag)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/#post-10860866)
 * So why isn’t it working that way then?
 * I intend to do this properly later, using the method you linked, but I was hoping
   to use an expedient method for testing purposes, so I can experiment with the
   code.
    -  This reply was modified 7 years, 7 months ago by [starwag](https://wordpress.org/support/users/starwag/).
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/#post-10860883)
 * I suspect your problem is how the ifs are nesting. I’d try removing a lot of 
   the cases and adding them back in, one at a time, until you figure it out. (multi-
   test/fail ifs make me crazy)
 *  Thread Starter [starwag](https://wordpress.org/support/users/starwag/)
 * (@starwag)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/#post-10860886)
 * Sounds like good advice. I’ll give it a try. Thanks!

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

The topic ‘Adding Javascript Functions to Header.php’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [theme development](https://wordpress.org/support/topic-tag/theme-development/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 2 participants
 * Last reply from: [starwag](https://wordpress.org/support/users/starwag/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/adding-javascript-functions-to-header-php/#post-10860886)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
