Add_filter on image upload only for product image

We're shifting our Forum based support to a more dedicated support system!

We'll be closing our Forum support from 10th June, 2020 and move to Email Support assistance.

  • If you are a WCFM premium add-ons user, contact us- here
  • Want to know more before buying our add-ons? Send Pre sale queries- here
  • If you are a WCFM free user, please open a support ticket at WordPress.org
  • For WCFM App related queries, reach us- here
From now the forum will be read-only!

Viewing 10 reply threads
  • Author
    Posts
    • #123947
      catou67
      Participant

      I have written a filter with the hoock ‘wp_handle_upload’ .
      I would like to apply this filter only on product image
      If I can identify which collaspe-content is displayed may be it could be sufficient

      Thank’s for help

    • #124051
      Sushobhan
      Keymaster

      Hi,
      Thanks for getting in touch with us!
      Are you talking about the vendor ‘add product’ screen? Also I’ll need to know the purpose of your code otherwise it would be difficult for me to guide you.
      If you want to locate the portion of code responsible for displaying the image section in Add/edit product page, open the file wc-frontend-manager\views\products-manager\wcfm-view-products-manage.php and search for the filter wcfm_product_manage_fields_images.
      Looking forward to helping you.
      Thank You!

    • #124095
      catou67
      Participant

      Hi,
      Thanks for your answer.

      My target is to build a square image for each product, including the FULL ORIGINAL image, that with adding blanck part on width or height.
      No problem to write this code, my problem is to not apply this code to any uploaded image, but only for products image.

      I add add_filter on ‘wp_handle_upload’ : that is ok

      add_filter(‘wp_handle_upload’, ‘cdpro_resize_image’, 10, 2 );

      function cdpro_resize_image( $array, $context ) {

      if ( I would like to apply this filter only on product image upload ) {

      $ok = array( ‘image/jpeg’, ‘image/gif’, ‘image/png’ );
      if ( ! in_array( $array[‘type’], $ok ) ) return $array;

      $im = imagecreatefromjpeg($array[‘file’]);
      $text_color = imagecolorallocate($im, 233, 14, 91);
      imagestring($im, 4, 5, 5, ‘Some text’ , $text_color);
      imagejpeg($im, $array[‘file’]);

      // Libération de la mémoire
      imagedestroy($im);

      }
      return $array;

      }

      Thank’s for help

    • #124605
      Sushobhan
      Keymaster

      Hello,
      Thanks for the clarification. You can use the following logic to determine if it’s product edit page or not-

      $referrer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
      $wcfm_edit_product_url =  get_wcfm_edit_product_url();
      if( $referrer && strpos($referrer, $wcfm_edit_product_url) !== false ){
      //your code goes here
      }

      Let me know how it goes.
      Thank You!

    • #124830
      catou67
      Participant

      Hi,

      Thanks for your quick answer. It work’s “quite” well,

      I would like to apply my filter only when I load a product image not when I add a media to a post.

      How can I distinguish this 2 ways ?
      There is certainly a way to do it : into the view select image the button to select the picture is
      For a product image : “select”
      For a media : “insert into product”

      Thank’s for help

      Attachments:
      You must be logged in to view attached files.
    • #126226
      catou67
      Participant

      Hi,

      I still search a solution to my problem. With no success after few hours !
      I have written a filter (added to wp_handle_upload) which modify the uploaded image to keep the full original image into a square 500*500.
      It’s work fine, image is not streched but blank area is added on top/buttom or left/right if the original imag is not a square image.
      The filter is applied only in wcfm_edit_product
      BUT
      It is fine when I load the picture of the product but the same filter is done whe an image is uplaoded in the short or long description

      I don’t find how to distinguish these 2 ways

      Any idea ??

      Thank’s for help I am near the target

    • #126362
      Sushobhan
      Keymaster

      Hi,
      There is no ready-made option to distinguish between these two case. Give me sometime to look for all the possibilities.
      I’ll keep you posted, please be patient.
      Thank You!

    • #126407
      catou67
      Participant

      Thank’s for your response, I understand that this subject is not simple, I have spent somme hours with no success
      Sorry to have disturbed you.
      Bravo for the great job you do to help us
      Regards

    • #128130
      catou67
      Participant

      Hi,
      I have solved my problem.
      In fact I have written a java/php function associated to each “load media” instance (onclick). This function stores a flag in the database.
      Flag which is tested when an upload image is done. So I know if the filter has to be applied.
      It’s work fine .

      Regards

    • #128365
      Sushobhan
      Keymaster

      Are you are storing a single key in options or user meta table? How it handles concurrent requests of uploads? There might be a race condition even from a single vendor, if she/he uses multiple tabs at once. Say, the vendor clicks on the image to open the WP media uploader, switch tabs, then click on media button above description field (More than one add product pages open in tabs). Again go back to previous tab and complete the product image upload. Will not that cause an issue?
      I don’t know your implementation but this thing first comes to my mind, so I ask!
      Thank you for keeping me updated.

    • #128538
      catou67
      Participant

      Hi,

      I add a meta_key to user meta table. I know that my solution has limits, but considering my vendors it’s suffisant at this time.
      I will try to do better if the need arises.

      Thank you for your help

Viewing 10 reply threads
  • You must be logged in to reply to this topic.