Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>wp_enqueue_script is the right way of doing it. You could do the same thing Scott suggested, but the purpose of wp_enqueue_script is to avoid conflicts, which is something WP automatically handles. </p> <p>Are you loading this on the front end or back end? </p> <p>If back-end, I highly recommend limiting your JS files to only loading on your plugin pages, here's how you do that:</p> <pre><code>add_action('admin_menu', 'add_pages'); function add_pages() { $my_plugin_page = add_menu_page('Plugin Name', 'Plugin Name', 8,__FILE__, 'invoice_overview',"images/plugin_icon.png"); // This is the trick, notice the "$my_plugin_page" being tacked on the end of admin_print_scripts add_action( "admin_print_scripts-$my_plugin_page", 'admin_head'); } function admin_head() { global $path_to_your_files; // You may notice array('jquery') at the end, that means jQuery is required to make that particular file function, and WP will include it automatically wp_enqueue_script('jquery.whatever',$path_to_your_files . "/js/jquery.whatever.js", array('jquery')); wp_enqueue_script('jquery.autocomplete',$path_to_your_files . "/js/jquery.autocomplete.js", array('jquery')); wp_enqueue_script('your_custom_file',$path_to_your_files . "/js/your_custom_file.js", array('jquery')); } </code></pre> <p>For the front-end its more difficult to isolate your JS files to specific pages. The correct way to insert js files is similar, except I typically load them into the init, just as you did. </p> <p>One more thing, be careful using __construct in your PHP code because it doesn't work in PHP4, in case you plan on distributing your plugin, keep in mind some people are still using PHP4.</p> <p>Good luck.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload