Osclass hooks explained, how to add and run osclass hooks

A hook is a small piece of code that allows you to insert more code (plugin) in the middle of certain Osclass’ actions. For instance we want a certain job or functionality to be triggred when the user has registered or when the user has posted a new item and you want to send an email to the user that’s when hooks come into play.

Hooks are very useful when you are building osclass plugins. It will help you achieve great functionality in your osclass plugin. Hooks are trigged even when your plugin is installed and uninstalled. There are many osclass build in hooks you can use while building plugins.

How to add hooks ?

Osclass has a inbuilt function name osc_add_hook('hook_name', 'function_name'); which will help you trigger the hook.

<?php
function my_simple_fucntion(){
echo 'this is a sample hook';
}
osc_add_hook('before_main', 'my_simple_function');

We have created a function my_simple_function() and it will do great things like printing the string. Wrap all the functionality in you function and add the hook with the function osc_add_hook(). The first parameter to the osc_add_hook() function is the hook name “before_main” and the second parameter is the name of the function "my_simple_function".

Hooks run on the osclass core file and the osclass themes you install. The “before_main” hook we have used in the example runs before the main body of the theme. When we added the hook the function we passed to the hook will execute and will print the string. Similarly you can use all the inbuilt hooks provided by osclass.

Special osclass hooks

There are some special hook that run on osclass. i.e “_uninstall” and “_configure”

_unintall hook

You can add and use this hook in your plugin. This hook will run when your plugin is being unintalled. For instance you want to reset OR delete the plugin preference on uninstall

<?php
function reset_plugin_preference(){
//insert you code to reset or delete the preference here
osc_delete_preference('name', 'section');
}
osc_add_hook(__FILE__ . "_uninstall", 'reset_plugin_preference');

_configure

This hook will show a configure link in the plugins table. You can use this hook to add a link to the plugins settings / configuration page if your plugin does not need much configuration. There are other ways to add settings / configuration links in the admin panel menu as well.

<?php
function add_config_page(){
//add you settings / configuration file path here
osc_admin_render_plugin('your_plugin_folder/setting.php');
}
osc_add_hook(osc_plugin_path(__FILE__)."_configure", 'add_config_page');

We have created a function add_config_page(), this function will render the settings/configurations page for the plugin. The function internally uses osc_admin_render_plugin() function which will display the settings/configuration page.

The path to you settings/configuration page is passed as the parameter to the osc_admin_render_plugin(). Then we have added the hook “_configure” prepended by the plugins path which will add the configure link in the plugins table.

configure link in the plugins table
configure link in the plugins table

You can look for all the available hooks at osclass documentation website.

Adding custom osclass hooks

In some special case we want to run a function in our plugin. Osclass hooks are not limited to the inbuilt ones, you can add and run your custom hooks. The custom hooks are not limited to your plugin if your plugin is installed them the other plugins and themes can use your hook.

<?php
/*----------------------------------
* You beautiful and elegant PHP code 
* goes in here
*----------------------------------/

osc_run_hook('my_custom_hook');

/*----------------------------------
* You beautiful and elegant PHP code 
* goes in here
*----------------------------------/

osc_run_hook() function will let run custom hooks in your themes and plugins. Only the name of the hook is passed as the parameter to the osc_run_hook() function. Now it time to add the hook in you theme or plugin.

<?php
function do_something(){
//you code goes in here
echo 'my custom hook is working';
}
osc_add_hook('my_custom_hook', 'do_something');

Running custom hooks is just as same as running inbuilt hooks we just pass the custom hook name and the function as parameters.

Hope this article has cleared your mind on how run and add osclass hooks.  All the osclass inbuilt hook will be explained in the next post. Stay tuned.

Leave A Reply

Navigate