My first WordPress plugin - Can't get script to run

大兔子大兔子 提交于 2019-12-12 02:17:21

问题


I thought this would be really simple but have spent a few days and am stuck.

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

Script and form is here: http://jsfiddle.net/p8j3T/2/

Live page is here: http://www.northwindcomputing.com/ndm-mini-storage-calc-test/

Thanks!!


回答1:


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

http://www.northwindcomputing.com/wp-content/plugins/ndm-mini-storage-calc/ndm-mini-storage-calc/ndm-mini-storage-calculator.js?ver=3.9

While the script is located at

http://www.northwindcomputing.com/wp-content/plugins/ndm-mini-storage-calc/ndm-mini-storage-calculator.js?ver=3.9

the plugin path is added twice.



来源:https://stackoverflow.com/questions/23132447/my-first-wordpress-plugin-cant-get-script-to-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!