Title: Need help with optimization
Last modified: May 12, 2022

---

# Need help with optimization

 *  [Натали Иваново – Ивановский трикотаж оптом](https://wordpress.org/support/users/natali37/)
 * (@natali37)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-optimization/)
 * I create my plugin for import products in woocommerce.
    I get data about products
   through the open API of my site.
 * The plugin works fine in a local environment and on most servers.
 * But there are customers who have encountered a problem. When uploading images
   for a product, an error occurs:
    `PHP Fatal error: Out of memory (allocated 232787968)(
   tried to allocate 33554440 bytes) in wp-includes/functions.php on line 2631`
 * The error completely interrupts the script. At the same time, for some time this
   client did not have such a problem, and he was able to unload more than 7000 
   products without any problems
 * Image loading function
 *     ```
       public static function saveFiles($arFiles, $type = 'previewUrl', $post_id = 0)
           {
               $settings = new Natali_Model_Settings();
               $isSingle = (int)$settings->get('image_color');
   
               try {
                   $localFiles = [];
   
                   foreach ($arFiles as &$file) {
                       if ($type === 'previewUrl') {
                           $url = str_replace(
                               'https://static.natali37.ru/',
                               'https://static.natali37.ru/media/600/',
                               $file['url']);
                       } else {
                           $url = $file['url'];
                       }
   
                       if ($isSingle) {
                           if ($file['mainColorImage']) {
                               $localFiles[crc32($url)] = self::getFileID($url, $post_id);
                           }
                       } else {
                           $localFiles[crc32($url)] = self::getFileID($url, $post_id);
                       }
                   }
   
                   return self::$files = $localFiles;
               } catch (\Exception $error) {
                   Natali_Log::set($error->getMessage(), $error->getCode());
               }
           }
       ```
   
 *     ```
       private static function getFileID($url, $post_id = 0)
           {
               if ($existId = self::findByName($url)) {
                   return $existId;
               }
   
               return media_sideload_image($url, $post_id, null, 'id');
           }
       ```
   
 *     ```
       private static function findByName($localFile)
           {
               $arFile = explode('/', $localFile);
   
               [$fileName, $fileExt] = explode('.', array_pop($arFile));
               $find = new \WP_Query([
                       'post_type' => 'attachment',
                       'name'      => $fileName
               ]);
   
               if ($find->have_posts()) {
                   $attachments = $find->get_posts();
                   foreach ($attachments as $attach) {
                       return $attach->ID;
                   }
               }
   
               return false;
           }
       ```
   
 * And although the error occurs. Almost all images get into media files and are
   attached to the product. But since the code is interrupted by an error. Then 
   I fail to insert images into the product itself through the command
 *     ```
        @var $this->wc_api \Automattic\WooCommerce\Client
       $this->wc_api->put("products/$this->wc_id", $this->wc_data)
       ```
   
 * You can view the full plugin code [https://wordpress.org/plugins/natali/](https://wordpress.org/plugins/natali/)
    -  This topic was modified 4 years, 1 month ago by [Натали Иваново - Ивановский трикотаж оптом](https://wordpress.org/support/users/natali37/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Benedikt Ledl](https://wordpress.org/support/users/benniledl/)
 * (@benniledl)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-optimization/#post-15639200)
 * Hi,
    you can deactivate plugins that you don’t need for example imagick.so. Also
   you can rise the php memory limit if you have enough included in your hosting
   plan.
 * Both in php.ini
 *  Thread Starter [Натали Иваново – Ивановский трикотаж оптом](https://wordpress.org/support/users/natali37/)
 * (@natali37)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-optimization/#post-15639557)
 * Php.ini memory_limit = 1024M
    Unfortunately I can’t control the active plugins
   and settings of all plugin users
 * Are there any recommendations for using the media_sideload_image function. And
   disabling other plugins for on is not an option. Since our plugin must, in addition
   to importing goods, check the data of goods every day, add new products, delete
   old ones, etc.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Need help with optimization’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Натали Иваново – Ивановский трикотаж оптом](https://wordpress.org/support/users/natali37/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/need-help-with-optimization/#post-15639557)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
