Advertisement:

Author Topic: Load a plugin in item-post.php only when a user selects certain category  (Read 730 times)

Resta

  • Sr. Member
  • ****
  • Posts: 345
Hello,

Just like loading custom fields for certain categories, I am looking for a way to load plugin fields in the item-post.php only when the user selects a certain category and its subcategories. Is this possible? Could someone please help with this? Thanks!

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Load a plugin in item-post.php only when a user selects certain category
« Reply #1 on: October 07, 2018, 11:02:01 am »
Hello,

Isn't that what the attribute plugins do? For example "Product Attributes", you select plugin categories in admin and then the fields show when category selected in item post.

Regards.

calinbehtuk

  • Sr. Member
  • ****
  • Posts: 450
Re: Load a plugin in item-post.php only when a user selects certain category
« Reply #2 on: October 07, 2018, 12:19:06 pm »
Code: [Select]
// my-plugin-name = the Short Name of the plugin

//add fields
function my_plugin_item_post($catId = null) {
    if (osc_is_this_category('my-plugin-name', $catId)) {
        require_once'your file';
    }
}

function my_plugin_item_edit($catId = null, $itemId = null) {
    if (osc_is_this_category('my-plugin-name', $catId)) {
        require_once'your file';
    }
}

//post
osc_add_hook('item_form', 'my_plugin_item_post');
//edit
osc_add_hook('item_edit', 'my_plugin_item_edit');

//set categories for this plugin
function my_plugin_configure_categories() {
    osc_plugin_configure_view(osc_plugin_path(__FILE__));
}

osc_add_hook(osc_plugin_path(__FILE__) . "_configure", 'my_plugin_configure_categories');

Resta

  • Sr. Member
  • ****
  • Posts: 345
Re: Load a plugin in item-post.php only when a user selects certain category
« Reply #3 on: October 08, 2018, 09:17:46 am »
Patrick - thanks some plugins don't have that setup to configure categories.

Clainbehtuk,

Thank you - I tried this for the tags plugin and it still loads the form for any category even though I configured one category/subcategories from the admin area. I also disabled (not uninstall) the plugin and reloaded it, I cleared the cache/content and tried again but still it will load the form when any category is selected.

Can you please see if I did anything wrong?

Code: [Select]
function my_plugin_item_post($catId = null) {
    if (osc_is_this_category('tags', $catId)) {
            require_once( TAGS_PATH . 'item_form.php' ) ;
    }
}

function my_plugin_item_edit($catId = null, $itemId = null) {
    if (osc_is_this_category('tags', $catId)) {
            require_once( TAGS_PATH . 'item_form.php' ) ;
    }
}

//post
osc_add_hook('item_form', 'tags_form');
//edit
osc_add_hook('item_edit', 'tags_edit_form');

//set categories for this plugin
function my_plugin_configure_categories() {
    osc_plugin_configure_view(osc_plugin_path(__FILE__));
}
osc_add_hook(osc_plugin_path(__FILE__) . "_configure", 'my_plugin_configure_categories');

Also, if I can get this to work - is it also possible to move this plugin field right below the categories dropdown?

Thanks again.
« Last Edit: October 08, 2018, 09:27:45 am by Resta »

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Load a plugin in item-post.php only when a user selects certain category
« Reply #4 on: October 08, 2018, 11:17:22 am »
Patrick - thanks some plugins don't have that setup to configure categories.

Clainbehtuk,

Thank you - I tried this for the tags plugin and it still loads the form for any category even though I configured one category/subcategories from the admin area. I also disabled (not uninstall) the plugin and reloaded it, I cleared the cache/content and tried again but still it will load the form when any category is selected.

Can you please see if I did anything wrong?

Code: [Select]
function my_plugin_item_post($catId = null) {
    if (osc_is_this_category('tags', $catId)) {
            require_once( TAGS_PATH . 'item_form.php' ) ;
    }
}

function my_plugin_item_edit($catId = null, $itemId = null) {
    if (osc_is_this_category('tags', $catId)) {
            require_once( TAGS_PATH . 'item_form.php' ) ;
    }
}

//post
osc_add_hook('item_form', 'tags_form');
//edit
osc_add_hook('item_edit', 'tags_edit_form');

//set categories for this plugin
function my_plugin_configure_categories() {
    osc_plugin_configure_view(osc_plugin_path(__FILE__));
}
osc_add_hook(osc_plugin_path(__FILE__) . "_configure", 'my_plugin_configure_categories');

Also, if I can get this to work - is it also possible to move this plugin field right below the categories dropdown?

Thanks again.

Replace " osc_add_hook('item_form', 'tags_form'); " with " osc_add_hook('item_form', 'my_plugin_item_post'); " and " osc_add_hook('item_edit', 'tags_edit_form'); " with " osc_add_hook('item_edit', 'my_plugin_item_edit'); ".

Regards.

Resta

  • Sr. Member
  • ****
  • Posts: 345
Re: Load a plugin in item-post.php only when a user selects certain category
« Reply #5 on: October 08, 2018, 01:46:27 pm »
Hi Patrick,

 had it first like that - as you suggested below ie. osc_add_hook('item_form', 'my_plugin_item_post'); and it did not work, so I wanted to try (not very smart) the way the hooks are in the plugin index.php file.  Anyway, the issue not solved - t still loads the tags form for any/all categories.
« Last Edit: October 08, 2018, 01:49:27 pm by Resta »

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Load a plugin in item-post.php only when a user selects certain category
« Reply #6 on: October 08, 2018, 02:42:50 pm »
Can you please post your whole index.php?

Regards.

Resta

  • Sr. Member
  • ****
  • Posts: 345
Re: Load a plugin in item-post.php only when a user selects certain category
« Reply #7 on: October 09, 2018, 08:22:16 am »
too big
« Last Edit: January 04, 2019, 05:52:33 am by Resta »