2015-10-07 38 views
0

我在排队脚本时遇到了麻烦。我不确定它为什么不起作用。我知道脚本代码本身的作品。WordPress的:我如何正确排入此JavaScript文件?

另一个问题 - 如果我试图包含这个.js文件,js代码本身是否应该被<script></script>包围?

编辑 - **我忘了提及我正在制作一个扩展到一个现有的插件。我想编写排队代码在我的扩展的functions.php文件中。我该怎么做呢?以下是我的设置。 **注意这是给我的错误:Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Find_Do_For_Anspress' does not have a method 'fd_enqueue_front' in C:\wamp\www\wordpress\wp-includes\plugin.php on line 503


我的设置

anspress个问题,answer.php

require_once __DIR__ . '/../find-do-for-anspress/find-do-for-anspress.php'; //I left out most of the other code but this correctly includes the file below 

发现-DO换anspress。 php

public function includes() { 
     require_once(FIND_DO_FOR_ANSPRESS_DIR.'/functions.php'); // Again, I'm keeping it short and simple 
    } 

的functions.php

add_action('wp_enqueue_scripts', array($this, 'fd_enqueue_front')); 
function fd_enqueue_front() 
    { 
     wp_enqueue_script('braintree-js', plugin_dir_url('braintree-js.js') . 'braintree-js.js', array()); 
    } 

布伦特里-js.js

<script src="https://js.braintreegateway.com/v2/braintree.js"></script> 
<script> 

var clientToken = "12345"; 

braintree.setup(clientToken, "dropin", { 
    container: "payment-form" 
}); 
</script> 
+0

为什么你还没有尝试使用'plugins_url()'来获取'find-do-for-anspress'插件的url? –

+0

plugins_url(__FILE__)为您带来当前插件文件,然后您可以轻松排入js。 –

+0

http://wordpress.stackexchange.com/questions/99067/enqueue-scripts-inside-a-class-in-a-plugin这个链接将帮助你获得。 –

回答

0

要排队脚本是一个两个步骤的过程。首先你排队你的脚本,然后注册保存它的函数。

像这样:

//Enqueue script in a function 
function theme_name_scripts() { 
    wp_enqueue_script('braintree-js', plugin_dir_url('braintree-js.js') . 'braintree-js.js', array()); 
} 
//Register the function 
add_action('wp_enqueue_scripts', 'theme_name_scripts'); 

See Codex了解更多详情。

在附注上;如果您要使用与运行文件相同的脚本排入文件,则可以使用plugin_dir_url(__FILE__)。但是现在您想要从排列另一个脚本中的文件,您必须指定文件名以轻松获取正确的目录。

相关问题