Advertisement:

Author Topic: Block item post/edit if no photo included  (Read 631 times)

Brutal

  • Newbie
  • *
  • Posts: 15
Block item post/edit if no photo included
« on: November 02, 2017, 10:37:10 pm »
I am looking for a way to make picture uploads mandatory, where I would like to require at least one photo be included on listings. I was looking at the hooks and can insert code when they are posted/edited but I don't believe OSClass allows for me to conditionally block an action from a hook.

I guess as this is a two part problem, I am curious how I can (A) check for the number of photos that have been placed in the listing and (B) block the post/edit action with error message if it there is not at least 1 photo. I'm thinking these two hooks may be my best option but I am not entirely sure if they will work.

posted_item : Run when an item is posted. $item is passed as argument
edited_item : Run when an item is edited. $item is passed as argument

calinbehtuk

  • Sr. Member
  • ****
  • Posts: 450
Re: Block item post/edit if no photo included
« Reply #1 on: November 03, 2017, 11:54:24 pm »
In the file function.php from your theme
Before the ending php tag ?> if you have one
Code: [Select]
function check_the_image_on_post($item) {
    if (empty($item['photos'])) {
        osc_add_flash_error_message(__('Please add at least one image.', 'bender'));
        $url = osc_item_post_url_in_category();
        header("Location: $url");
        exit();
    }
}

osc_add_hook('pre_item_add', 'check_the_image_on_post');

function check_the_image_on_edit($item) {
    if (empty($item['photos'])) {
        osc_add_flash_error_message(__('Please add at least one image.', 'bender'));
        $url = osc_item_edit_url();
        header("Location: $url");
        exit();
    }
}

osc_add_hook('pre_item_edit', 'check_the_image_on_edit');

Brutal

  • Newbie
  • *
  • Posts: 15
Re: Block item post/edit if no photo included
« Reply #2 on: November 04, 2017, 02:37:55 am »
Ah that is perfect, thank you so much!

pixelpadre

  • Jr. Member
  • **
  • Posts: 76
Re: Block item post/edit if no photo included
« Reply #3 on: December 17, 2018, 03:30:25 pm »
Adding custom field forces them to acknowledge listing will be deleted in 24 hours.

https://forums.osclass.org/themes/photo-is-required-field/15/