June 17, 2015

Wordpress define your javascript right way

Sometime you see in Wordpress line of code like: wp_enqueue_script( 'swfobject' );

This is the way to call javascript already in Wordpress but how to define it? I will help you to use it.

Have many ways to insert external javascript file into Wordpress theme. But you should use way below to register new javascript file and add it to your template when needed.

Here is code:

Firstly, use "wp_register_script" function to define your file:

wp_register_script( 'qtobject', plugins_url('/path/to/your/file.js'), array(), '1.0.0' );

"qtobject" is script name handle

Then enqueues script anywhere in template:

wp_enqueue_script( 'qtobject' );