Share icon

While creating wordpress plugin, often we need javascript to use extend the user experience. In this tutorial let see how we can load javascript in wordpress plugin

we Assume you how to create custom wordpress plugin and on your main plugin php file you need to enqueue external scripts.There are various ways to do that like

  1. Enqueue script using wordpress enqueue function
  2. Use the wp_footer or wp_head hooks to add the script inline
  3. Use a plugin to add header or footer scripts

In this article we are talking with the first way enqueue script using wp_enqueue_scripts action, 

1. Create your Js file, in plugin folder or create a js folder and placed into that, let assume file name custom.js

2. Add following code to main php file with action


function customize_myplugin_js_init() {
    wp_enqueue_script( 'your-plugin-name', plugins_url( '/js/custom.js', __FILE__ ),array('jquery'),1.0);

3. Put alert into you JS file to check it load on your action or not.

This is the right way to add javascript to into your wordpress plugin. for more info about wp_enqueue_script, check the link



Add new comment

Restricted HTML