Monday, 27 October 2014

How to insert a script file in osclass admin.

The enqueue functions are used for loading your javascript and css files in osclass admin.The reason for using enqueue functions is becuse many plugins are using javascript frameworks such as JQuery, JQuery-ui, and fancybox just to name a few. There is a chance that these javascript files could be loaded multiple times and that is why the enqueue functions are used. The enqueue functions help to reduce the chances of the same file getting loaded more than once in osclass. 

Here, I have explained with examples below about how to insert a script file in osclass admin when we creating a plugin.
Registering a javascript file
osc_register_script('jqueryPopup', osc_base_url() . 'oc-content/plugins/popup_for_osclass/js/jCarouselPopup.js', 'jquery');
Enqueueing the script
osc_enqueue_script('jqueryPopup');
Enqueueing a style
osc_enqueue_style('popuplCss', osc_base_url() . 'oc-content/plugins/popup_for_osclass/css/popup.css');
You may have noticed that i did not register the style and that is because it is not required and there is no function to do so.Real life example,for inserting a script file in osclass admin.Ok now let us move to a real life example. To use these functions you have to create a function for this example I am going to use examle_load_scripts() please use a unique function name in your plugin if you are planning on releasing it otherwise we could end up with conflicting function names.

function examle_load_scripts() {
     osc_register_script('jqueryPopup', osc_base_url() . 'oc-content/plugins/popup_for_osclass/js/jCarouselPopup.js', 'jquery');
     osc_enqueue_script('jqueryPopup');
     osc_enqueue_style('carouselCss', osc_base_url() . 'oc-content/plugins/popup_for_osclass/css/popup.css');
 }
Once you add that code you will then need to add the following hook to your index.php file of your plugin. 

osc_add_hook('init', 'examle_load_scripts');
To load the script on the admin side you would use this hook

osc_add_hook('init_admin', 'examle_load_scripts');
This is the way of inserting a script or a css file into osclass admin.