Site icon Osclass Documentation & Knowledge Base

Item publish & edit hook variants

In Osclass v8.2.0, we brought possibility to use multiple category-based hooks for item publish and item edit pages. Until now, there was one and only one hook for publish/edit, that was very limiting. This hook was usually shown at the end of publish/edit page. (Keep in mind default one still need to exists on publish/edit pages!).

Predefined variants (if your theme has these integrated already):

To use predefined variant, you just hook your function to it.

Warning! Not meant to use all of these. Each variant generates 1 ajax call.

Function for publish page:

ItemForm::plugin_post_item();

Function for edit page:

ItemForm::plugin_edit_item();

Hook and parameter variants

In new version, we added optional parameters to these 2 functions:

Hook variant

Let’s say you want to create new category-based hook and show data right after category selection. You do not want to impact current functions in any way.

1) Create custom function that shows some data (does not need to be category related):

function my_custom_func($catId) {
  echo '<div style="padding:15px 15px 15px 30%;background:#ccc;display:inline-block;width:100%;margin:0 0 10px 0;">';
  echo 'Category ID is: ' . $catId;
  echo '</div>';
}

2) Think about unique variant to use and hook this function to item publish and edit page. Let’s use variant “vcategory”. You will hook new function using these commands:

osc_add_hook('item_form_vcategory', 'my_custom_func');
osc_add_hook('item_edit_vcategory', 'my_custom_func');

3) Update your theme item-post.php (maybe item-edit.php is required as well!). We consider theme use just item-post.php. You want to show new data below category selection.

a) Place following code below category box (note ie on sigma theme this would be around line 70, not needed if theme has variants integrated already and you want to use existing one):

<?php
  if(Params::getParam('action') == 'item_edit') {
    ItemForm::plugin_edit_item('vcategory');
  } else {
    ItemForm::plugin_post_item('vcategory');
  }
?>

b) Alternative way is to use new design hooks (if supported by your theme) and show box using hook:

osc_add_hook('item_publish_category', function() {
  if(Params::getParam('action') == 'item_edit') {
    ItemForm::plugin_edit_item('vcategory');
  } else {
    ItemForm::plugin_post_item('vcategory');
  }
});

Summary – whole code you can place ie. to theme functions.php:

function my_custom_func($catId) {
  echo '<div style="padding:15px 15px 15px 30%;background:#ccc;display:inline-block;width:100%;margin:0 0 10px 0;">';
  echo 'Category ID is: ' . $catId;
  echo '</div>';
}

osc_add_hook('item_form_vcategory', 'my_custom_func');
osc_add_hook('item_edit_vcategory', 'my_custom_func');

osc_add_hook('item_publish_category', function() {
  if(Params::getParam('action') == 'item_edit') {
    ItemForm::plugin_edit_item('vcategory');
  } else {
    ItemForm::plugin_post_item('vcategory');
  }
});

Now you should see something like this (sigma):

Note: if your theme has these hooks integrated, code related to calling ItemForm functions is not required at all! Do not add it, otherwise you will get duplicated results.

Parameter variants

Parameter variants allows to parametrize existing function hooked to item_form and item_edit. In this case no new hooks are used, but parameter “variant” is available.

Params::getParam('variant')

It’s equal to empty string if no parameter variant is provided.

In this case, you need to place new hooks in following form:

if(Params::getParam('action') == 'item_edit') {
  ItemForm::plugin_edit_item('', 'pcategory');
} else {
  ItemForm::plugin_post_item('', 'pcategory');
}

Note that you cannot use parameter variants and hook variants at same time. Hook variant will be prioritized and parameter variant ignored.