Sppidy
Sppidy

Reputation: 423

How to link external JavaScript file to WordPress theme?

I can't link the external JavaScript file to my WordPress theme through the plugin. All I found is this way to attach a JavaScript file:

function theme_name_scripts() {
    wp_register_script('script-name-faizan-test', 'http://vjs.zencdn.net/4.12/video.js');
    wp_enqueue_script('script-name-faizan-test');
}

add_action('wp_enqueue_scripts', 'theme_name_scripts');

What am I doing wrong?

Upvotes: 0

Views: 1548

Answers (2)

A.Sharma
A.Sharma

Reputation: 2799

Check this link out:

Note that your relative path may be different.

function wptuts_scripts_basic()
  {
  // Register the script like this for a plugin:
  wp_register_script( 'custom-script', plugins_url( 'http://vjs.zencdn.net/4.12/video.js', __FILE__ ) );
  // or
  // Register the script like this for a theme:
  wp_register_script( 'custom-script', get_template_directory_uri() . 'http://vjs.zencdn.net/4.12/video.js' ); 

  // For either a plugin or a theme, you can then enqueue the script:
  wp_enqueue_script( 'custom-script' );
  }
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_basic' );

Upvotes: 0

Jake Bown
Jake Bown

Reputation: 411

According to the WP Codex, the example gives extra parameters:

<?php wp_register_script( $handle, $src, $deps, $ver, $in_footer ); ?>

So:

function child_add_scripts() {
    wp_register_script(
        'google-analytics',
        'http://google.com/analytics/script.js',
        false,
        '1.0',
        true
    );

    wp_enqueue_script( 'google-analytics' );

Upvotes: 1

Related Questions