{"id":664,"date":"2023-10-26T11:47:01","date_gmt":"2023-10-26T11:47:01","guid":{"rendered":"https:\/\/docs.osclasspoint.com\/?p=664"},"modified":"2023-11-07T19:46:41","modified_gmt":"2023-11-07T19:46:41","slug":"item-publish-edit-hooks-split","status":"publish","type":"post","link":"https:\/\/docs.osclasspoint.com\/item-publish-edit-hooks-split","title":{"rendered":"Item publish &#038; edit hook variants"},"content":{"rendered":"\n<p>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!).<\/p>\n\n\n\n<p><strong>Predefined variants<\/strong> (if your theme has these integrated already):<\/p>\n\n\n\n<ul><li>item_form_top<\/li><li>item_form_category<\/li><li>item_form_description<\/li><li>item_form_price<\/li><li>item_form_location<\/li><li>item_form_seller<\/li><li>item_form_images<\/li><li>item_form_hook<\/li><li>item_form_buttons<\/li><li>item_form_bottom<\/li><li>item_form_after<\/li><\/ul>\n\n\n\n<ul><li>item_edit_top<\/li><li>item_edit_category<\/li><li>item_edit_description<\/li><li>item_edit_price<\/li><li>item_edit_location<\/li><li>item_edit_seller<\/li><li>item_edit_images<\/li><li>item_edit_hook<\/li><li>item_edit_buttons<\/li><li>item_edit_bottom<\/li><li>item_edit_after<\/li><\/ul>\n\n\n\n<p>To use predefined variant, you just hook your function to it.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#fffdd6\"><strong>Warning!<\/strong> Not meant to use all of these. Each variant generates 1 ajax call.<\/p>\n\n\n\n<p>Function for publish page:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ItemForm::plugin_post_item();<\/pre>\n\n\n\n<p>Function for edit page:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ItemForm::plugin_edit_item();<\/pre>\n\n\n\n<h2>Hook and parameter variants<\/h2>\n\n\n\n<p>In new version, we added optional parameters to these 2 functions:<\/p>\n\n\n\n<ul><li><strong>$hook_variant<\/strong> &#8211; customize hook and use &#8220;item_form_{$variant}&#8221; and &#8220;item_edit_{$variant}&#8221; hook instead of default one<\/li><li><strong>$param_variant<\/strong> &#8211; pass custom parameter value to item_form &amp; item_edit hooks. Parameter name is &#8220;variant&#8221;.<\/li><\/ul>\n\n\n\n<h2>Hook variant<\/h2>\n\n\n\n<p>Let&#8217;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.<\/p>\n\n\n\n<p><strong>1) <\/strong>Create custom function that shows some data (does not need to be category related):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function my_custom_func($catId) {\n  echo '&lt;div style=\"padding:15px 15px 15px 30%;background:#ccc;display:inline-block;width:100%;margin:0 0 10px 0;\"&gt;';\n  echo 'Category ID is: ' . $catId;\n  echo '&lt;\/div&gt;';\n}<\/pre>\n\n\n\n<p><strong>2)<\/strong> Think about unique variant to use and hook this function to item publish and edit page. Let&#8217;s use variant &#8220;vcategory&#8221;. You will hook new function using these commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">osc_add_hook('item_form_vcategory', 'my_custom_func');\nosc_add_hook('item_edit_vcategory', 'my_custom_func');<\/pre>\n\n\n\n<p><strong>3)<\/strong> 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.<\/p>\n\n\n\n<p><strong>a) <\/strong>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):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\n  if(Params::getParam('action') == 'item_edit') {\n    ItemForm::plugin_edit_item('vcategory');\n  } else {\n    ItemForm::plugin_post_item('vcategory');\n  }\n?&gt;<\/pre>\n\n\n\n<p><strong>b)<\/strong> Alternative way is to use new design hooks (if supported by your theme) and show box using hook:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">osc_add_hook('item_publish_category', function() {\n  if(Params::getParam('action') == 'item_edit') {\n    ItemForm::plugin_edit_item('vcategory');\n  } else {\n    ItemForm::plugin_post_item('vcategory');\n  }\n});<\/pre>\n\n\n\n<p>Summary &#8211; whole code you can place ie. to theme functions.php:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function my_custom_func($catId) {\n  echo '&lt;div style=\"padding:15px 15px 15px 30%;background:#ccc;display:inline-block;width:100%;margin:0 0 10px 0;\"&gt;';\n  echo 'Category ID is: ' . $catId;\n  echo '&lt;\/div&gt;';\n}\n\nosc_add_hook('item_form_vcategory', 'my_custom_func');\nosc_add_hook('item_edit_vcategory', 'my_custom_func');\n\nosc_add_hook('item_publish_category', function() {\n  if(Params::getParam('action') == 'item_edit') {\n    ItemForm::plugin_edit_item('vcategory');\n  } else {\n    ItemForm::plugin_post_item('vcategory');\n  }\n});<\/pre>\n\n\n\n<p>Now you should see something like this (sigma):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"527\" src=\"https:\/\/docs.osclasspoint.com\/wp-content\/uploads\/2023\/10\/image-1024x527.png\" alt=\"Publish hook variants\" class=\"wp-image-665\" srcset=\"https:\/\/docs.osclasspoint.com\/wp-content\/uploads\/2023\/10\/image-1024x527.png 1024w, https:\/\/docs.osclasspoint.com\/wp-content\/uploads\/2023\/10\/image-300x155.png 300w, https:\/\/docs.osclasspoint.com\/wp-content\/uploads\/2023\/10\/image-768x396.png 768w, https:\/\/docs.osclasspoint.com\/wp-content\/uploads\/2023\/10\/image-60x31.png 60w, https:\/\/docs.osclasspoint.com\/wp-content\/uploads\/2023\/10\/image-150x77.png 150w, https:\/\/docs.osclasspoint.com\/wp-content\/uploads\/2023\/10\/image.png 1128w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#fffbd6\"><strong>Note: <\/strong>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.<\/p>\n\n\n\n<h2>Parameter variants<\/h2>\n\n\n\n<p>Parameter variants allows to parametrize existing function hooked to item_form and item_edit. In this case no new hooks are used, but parameter &#8220;variant&#8221; is available.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Params::getParam('variant')<\/pre>\n\n\n\n<p>It&#8217;s equal to empty string if no parameter variant is provided.<\/p>\n\n\n\n<p>In this case, you need to place new hooks in following form:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if(Params::getParam('action') == 'item_edit') {\n  ItemForm::plugin_edit_item('', 'pcategory');\n} else {\n  ItemForm::plugin_post_item('', 'pcategory');\n}<\/pre>\n\n\n\n<p>Note that you cannot use parameter variants and hook variants at same time. Hook variant will be prioritized and parameter variant ignored.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,12,14,13],"tags":[208,209,210,207],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/posts\/664"}],"collection":[{"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/comments?post=664"}],"version-history":[{"count":6,"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":681,"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/posts\/664\/revisions\/681"}],"wp:attachment":[{"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/media?parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/categories?post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.osclasspoint.com\/wp-json\/wp\/v2\/tags?post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}