0

I'm creating a word press theme, in my functions.php file I have the following script (see below)

My aim is run this script to include the Google fonts, shiv and respond.js in the HTML 'Head' , in exactly the same way as a static site.

Do I need to call this function from the head of every WordPress page, or is there a more efficient way to achieve this goal?

If so, can someone demonstrate how this would work?

****Aim : I want to run this function for every PAGE, so that Google fonts, shiv and respond.js all appear in my HTML HEAD****

function jlc_scripts() {    
wp_enqueue_style( 'googlewebfonts', 'http://fonts.googleapis.com/css?family=Open+Sans;' );

    echo '<!--[if lt IE 9]>';
    echo '  <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>';
    echo '  <script stc="'. TEMPPATH.'/js/respond.min.js"></script>';
    echo '<![endif]-->';    
}
add_action( 'wp_enqueue_scripts', 'mf_scripts' ); 
1
  • why not just put it in the header.php file and include that file in every other page? Commented Mar 23, 2015 at 12:17

1 Answer 1

2

NO you have to add this to your themes's function.php, you also have to change the function name parameter to the actual function like this.

function jlc_scripts() {    
wp_enqueue_style( 'googlewebfonts', 'http://fonts.googleapis.com/css?family=Open+Sans;' );

    echo '<!--[if lt IE 9]>';
    echo '  <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>';
    echo '  <script stc="'. TEMPPATH.'/js/respond.min.js"></script>';
    echo '<![endif]-->';    
}
add_action( 'wp_enqueue_scripts', 'jlc_scripts' ); 

Secondly for your script files there is a function similar to wp_enqueue_style, its called wp_enqueue_script. The parameters for this functions are a bit different.

wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );

The third parameter is important as you can add dependencies in there. For instance if your script requires jquery to be loaded before it, you can add jquery as the requirement. So that file will be added after the jquery is loaded. The last paremeter defines if you need to add your file to the footer or not. In your case it would become

wp_enqueue_script( 'respond', get_template_directory_uri() . '/js/respond.min.js', array(), '1.0.0', true );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.