Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dianjuar/5c3fcbea09df3a2e585d6fe896b4c59a to your computer and use it in GitHub Desktop.
Save dianjuar/5c3fcbea09df3a2e585d6fe896b4c59a to your computer and use it in GitHub Desktop.
How to enqueue Scipts in WordPress, the rigth way

When you want to enqueue a script in WordPress you need the URL of the JS or CSS!.

To accomplish that, the best way is to use a constant that have the URL of your plugin

if ( !defined( 'XXXXXX_PLUGIN_URL' ) )
	define( 'XXXXXX_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

and then use it to tell the url like this.

XXXXXX_PLUGIN_URL.'js/something.js'

For e.g:

wp_enqueue_script( 'some_script', # ID the the script to enqueue 
		   XXXXXX_PLUGIN_URL.'js/something.js', # Url
		   array( 'jquery' ), # Dependencies 
		   '12345', # version
		   true ); # I load on footer

Written with StackEdit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment