如何将外部JavaScript文件链接到WordPress主题?

时间:2015-09-04 13:38:35

标签: javascript php wordpress

我无法通过插件将外部JavaScript文件链接到我的WordPress主题。我发现所有这些都是附加JavaScript文件的方式:

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');

我做错了什么?

2 个答案:

答案 0 :(得分:0)

According to the WP Codex,该示例提供了额外的参数:

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

所以:

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

    wp_enqueue_script( 'google-analytics' );

答案 1 :(得分:0)

检查此link

请注意,您的相对路径可能不同。

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' );