有人可以告诉我如何将这个
javascript文件加入我的wordpress插件.我已经尝试过所有的wp_enqeue_script()方法,但没有任何反应.
<?PHP /* Plugin Name: Ava Test Plugin URI: http://#.com Description: A plugin that is used for my javascript tests Author: Ronny Kibet Author URI: http://ronnykibet.com version: 1.001 */ include(popup.js); /*when I include it this way,it works fine,but gives an error when I activate the plugin 'plugin generated 453 characters ...' */ function popup() { $src = plugins_url('popup.js',__FILE__); wp_register_script( 'popup',$src ); wp_enqueue_script( 'popup' ); } /* when I included it this way,plugin is activated but nothing happens. */ ?>
这是popup.js
<script type="text/javascript"> function popup(){ alert('hello there this is a test popup') } </script> <body onload="popup()"> </body>
您需要指定负载何时发生,请尝试这样做.
<?PHP /* Plugin Name: Ava Test Plugin URI: http://#.com Description: A plugin that is used for my javascript tests Author: Ronny Kibet Author URI: http://ronnykibet.com version: 1.001 */ add_action('wp_enqueue_scripts','ava_test_init'); function ava_test_init() { wp_enqueue_script( 'ava-test-js',plugins_url( '/js/ava_test_.js',__FILE__ )); }
此外,您的JS中有错误,但是我在一些答案中看到了正确的版本,希望这有所帮助
更新:有一个名为wp_enqueue_scripts的钩子,如@brasofilo所提及的,应该用来代替init来加载脚本.