Item Post Hooks Osclass

Osclass has some hooks on post page that you can use to run your function to insert a value in the database or perform some checks each time an item is posted.

In this article, we will present you the action hooks that will run after the form is submitted.

To stop the  Osclass post process you can use:

osc_run_hook('pre_item_add', $aItem, $flash_error);

Like:

        function my_function_pre_post($aItem, $flash_error) {
            //some data that you can extract from $aItem variable
            $categoryId = $aItem['catId'];
            $userId = $aItem['userId'];

            //check if you have a flash message, most likely an error
            if ($flash_error) {
                osc_add_flash_error_message($flash_error); //add some error message
                header("Location: " . osc_item_post_url()); //redirect on post page
                exit(); //exit
            }

            //other type of example
            $categoryId = Params::getParam('catId'); //get the category
            $myField = Params::getParam('myfied'); //get my value from form

            if ($myFielg != 'some value') {
                osc_add_flash_error_message(__('Some error to show', 'my_plugin')); //add some error message
                header("Location: " . osc_item_post_url()); //redirect on post page
                exit();
            }
        }

        osc_add_hook('pre_item_add', 'my_function_pre_post');

The above function is just an example that you can use, but take notice that you have multiple options displayed in the same function, most likely you will use only one o that example.

You have the following variables:

$aItem => will contain all the item information from the form, but will not contain the information that is not related with osclass fields like fields added by you or fields added by plugins.

$flash_error => will contain all the flash messages trigger by osclass in the publishing process.

To insert an error message on the post process in Osclass you can use:

osc_apply_filter('pre_item_add_error', $flash_error, $aItem);

Like:

function my_function_new_message($flash_error, $aItem) {
    $myField = Params::getParam('myField');
    if(empty($myField)){
      $flash_error .= __('Som error here', 'my_plugin').PHP_EOL;  
    }
    
    return $flash_error;
}

osc_add_filter('pre_item_add_error', 'my_function_new_message');

You can use the above example to insert your error message on the post process if some fields added by your plugin are not filed, and this is the recommended way to do this. With the pre-post hook from the first example, you stop the process and make a redirect in some cases this is ok but if you want to display all the error messages from osclass and other plugins in the same time, use this filter.

Again we have some variables:

$aItem => will contain all the item information.

$flash_error => will contain all the flash messages trigger by osclass in the publishing process or inserted by other plugins.

To update values in the database for plugins after the publish process using:

osc_run_hook('posted_item', $item);

Like:

            function my_function_after_post($item){
                $itemId = $item['pk_i_id']; //get item id from $item variable that will contain all the date of the item
                
                //get my field data to insert in database for my plugin
                $myField = Params::getParam('myField');
                
                //do some action here with my data............
            }
            osc_add_hook('posted_item', 'my_function_after_post');

This hook will run after the item is published and the $item variable will contain all the data for the published item, the data from osclass fields but not from fields added by plugins. You have to retrieve the information added by plugins, this information is not in $item variable. You use this hook to insert the value in the database for your plugin base of item id or user id etc.

$item => will contain all the information that is saved in the database for the published item, this will not contain information from fields added by plugins.

Take notice that these functions need to be used in your index.php file from your plugin or functions.php file from your theme. This will work in files that are included or required in index.php or functions.php.

Subscribe
X

Subscribe!

Subscribe to get the latest listings, updates and special offers delivered directly in your inbox.