我的第一个WordPress插件-无法运行脚本 [英] My first WordPress plugin - Can't get script to run
问题描述
我认为这真的很简单,但是花了几天时间就陷入了困境.
I thought this would be really simple but have spent a few days and am stuck.
这个想法是通过简码在页面上插入一个表单;该表格根据用户输入计算总计.将脚本和表单放在单个HTML文件中时,它们可以很好地协同工作.在插件中(如您所见),JS和FORM文件分别被调用.当前,脚本会在输出中加载,表单会在具有短代码的页面上加载,但是CALC功能将无法在本地或在我的服务器上运行.这是我插件的基本功能:
The idea is to insert a form on a page via shortcode; the form calculates totals from user input. The script and form play fine together when placed in a single HTML file. In the plugin (as you will see), the JS and FORM file are called separately. Currently, the script loads in the output, the form loads on the page that has the shortcode, but the CALC function will not work, either locally or on my server. Here are the bare-bones functions from my plugin:
/* Register and enqueue the script*/
wp_register_script( 'ndm-mini-storage-calculator', plugins_url('/ndm-mini-storage-calc/ndm-mini-storage-calculator.js', __FILE__ ) );
wp_enqueue_script( 'ndm-mini-storage-calculator' );
/* Add function to load the form and add shortcode */
function show_calc_form(){
return $options = wp_remote_retrieve_body( wp_remote_get( plugins_url() . '/ndm-mini-storage-calc/mini-storage-calculator_noscript.html' ) );
}
add_shortcode('ndm-mini-calc', 'show_calc_form');
脚本和格式在这里: http://jsfiddle.net/p8j3T/2/
实时页面在这里: http://www.northwindcomputing.com/ndm-mini-storage-calc-test/
谢谢!
推荐答案
删除插件URL,plugins_url
将为您获取.
在init上加载文件也是一个好主意
Remove the plugin URL, plugins_url
will get that for you.
It's also a good idea to load files on init
add_action('init', 'load_ndm_mini');
function load_ndm_mini(){
wp_register_script( 'ndm-mini-storage-calculator', plugins_url('ndm-mini-storage-calculator.js', __FILE__ ) );
wp_enqueue_script( 'ndm-mini-storage-calculator' );
}
如果打开控制台,您会注意到该网址看起来像
If you open the console, you'll notice the url looks like
the plugin path is added twice.
这篇关于我的第一个WordPress插件-无法运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!