Excessive memory consumption on the server and PHP

This topic has 3 replies, 2 voices, and was last updated 1 day, 7 hours ago ago by Jack Richardson

  • Avatar: molo
    molo
    Participant
    January 26, 2026 at 13:12

    Good morning! (if you can call it that…)

    I’m writing out of sheer desperation, begging for any help you can offer. After thoroughly reviewing the website and disabling, deleting, and implementing every improvement I could think of, the server support (who are the only ones with access to the website logs) tells me it’s still consuming a huge amount of memory with excessive PHP calls.

    Regarding the website design, I’ve removed animations and any videos or GIFs that might be slowing it down.

    As for plugins, only those necessary for WooCommerce and the theme are activated. There are others for improving security that I’ve also left activated.

    I don’t know what else to do, and my job is hanging by a thread… I beg you for help.

    Content is visible for topic creator and
    support staff only.
    2 Answers
    Avatar: molo
    molo
    Participant
    January 26, 2026 at 17:19

    Good afternoon. They reviewed my code and gave me this alternative for functions.php.

    It’s running now, and the website is working perfectly, even scoring 70 on the Lighthouse performance test.

    What isn’t working correctly is the filter: when you filter by size, it shows all products in that size, even if that size is out of stock. Shouldn’t it only filter for products that are currently in stock?

    
    <?php
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 1001 );
    function theme_enqueue_styles() {
    	if (function_exists('etheme_child_styles')){
    		etheme_child_styles();
    	}
    }
    
    add_filter('etheme_product_grid_list_product_wrapper_element_attributes', function($attr, $product, $local_settings, $object) {
        static $video_data_cache = []; // Cache estático por petición
        
        $product_id = $product->get_id();
        
        // Verificar si ya procesamos este producto
        if (isset($video_data_cache[$product_id])) {
            return $attr . $video_data_cache[$product_id];
        }
        
        $product_video_thumbnail = etheme_get_option('product_video_thumbnail', false);
        
        // Salida temprana si no hay video thumbnail habilitado
        if ( !$product_video_thumbnail ) {
            $video_data_cache[$product_id] = '';
            return $attr;
        }
        
        // Verificar campo personalizado
        if ( etheme_get_custom_field('product_video_thumbnail', $product_id) == 'disable' ) {
            $video_data_cache[$product_id] = '';
            return $attr;
        }
        
        // Obtener videos con caching transiente
        $videos = get_transient('etheme_video_' . $product_id);
        if ($videos === false) {
            $videos = etheme_get_attach_video($product_id);
            set_transient('etheme_video_' . $product_id, $videos, 12 * HOUR_IN_SECONDS);
        }
        
        if ( empty($videos[0]) ) {
            $video_data_cache[$product_id] = '';
            return $attr;
        }
        
        // Obtener solo el primer video (limitar recursos)
        $video_attachments = get_posts(array(
            'post_type' => 'attachment',
            'include'   => $videos[0],
            'post_mime_type' => array('video/mp4', 'video/webm', 'video/ogg'),
            'fields'    => 'ids', // Solo IDs para ahorrar memoria
            'posts_per_page' => 1,
            'no_found_rows' => true // Optimizar query
        ));
        
        if ( empty($video_attachments) ) {
            $video_data_cache[$product_id] = '';
            return $attr;
        }
        
        // Procesar atributos del video
        $product_video_attrs = etheme_get_option('product_video_thumbnail_attributes', array('preload', 'loop'));
        $product_video_pause_on_hover = etheme_get_option('product_video_pause_on_hover', false);
        
        $additional_attrs = '';
        
        // Solo agregar atributos de hover si hay video
        if ( in_array('autoplay', $product_video_attrs) && $product_video_pause_on_hover ) {
            $additional_attrs .= ' data-video-hover-state="pause"';
        } else if ( !in_array('autoplay', $product_video_attrs) ) {
            $additional_attrs .= ' data-video-hover-state="play"';
        }
        
        // Solo serializar JSON si es necesario para slider
        if ( 'slider' == $local_settings['image_hover_effect'] ) {
            $size = $local_settings['image_size'] != 'custom' ? $local_settings['image_size'] : 'woocommerce_thumbnail';
            $image_list = $object->etheme_get_image_list($product, true, $size);
            
            if ( !empty($image_list) ) {
                $video_url = wp_get_attachment_url($video_attachments[0]);
                $video_slides = wp_json_encode(array(
                    'items' => array(
                        array(
                            'item_type' => 'video',
                            'src' => $video_url
                        )
                    )
                ));
                $additional_attrs .= ' data-videos="'. esc_attr($video_slides) . '"';
            }
        }
        
        $video_data_cache[$product_id] = $additional_attrs;
        return $attr . $additional_attrs;
    }, 10, 4);
    
    add_filter('etheme_product_grid_list_product_element_image', function($image, $product, $local_settings) {
        static $processed_videos = []; // Cache estático
        
        $product_id = $product->get_id();
        
        // Evitar procesamiento duplicado
        if (isset($processed_videos[$product_id])) {
            return $processed_videos[$product_id];
        }
        
        // Verificación rápida sin consultas globales
        $product_video_thumbnail = etheme_get_option('product_video_thumbnail', false);
        if ( !$product_video_thumbnail ) {
            $processed_videos[$product_id] = $image;
            return $image;
        }
        
        if ( etheme_get_custom_field('product_video_thumbnail', $product_id) == 'disable' ) {
            $processed_videos[$product_id] = $image;
            return $image;
        }
        
        // Obtener video desde cache transiente
        $videos = get_transient('etheme_video_' . $product_id);
        if ($videos === false) {
            $videos = etheme_get_attach_video($product_id);
            set_transient('etheme_video_' . $product_id, $videos, 12 * HOUR_IN_SECONDS);
        }
        
        if ( empty($videos[0]) ) {
            $processed_videos[$product_id] = $image;
            return $image;
        }
        
        // Obtener solo el video necesario
        $video_attachments = get_posts(array(
            'post_type' => 'attachment',
            'include'   => $videos[0],
            'post_mime_type' => array('video/mp4', 'video/webm', 'video/ogg'),
            'posts_per_page' => 1,
            'no_found_rows' => true
        ));
        
        if ( empty($video_attachments) ) {
            $processed_videos[$product_id] = $image;
            return $image;
        }
        
        $video = $video_attachments[0];
        $size = $local_settings['image_size'] != 'custom' ? $local_settings['image_size'] : 'woocommerce_thumbnail';
        
        // Obtener configuración
        $product_video_attrs = etheme_get_option('product_video_thumbnail_attributes', array('preload', 'loop'));
        $product_video_pause_on_hover = etheme_get_option('product_video_pause_on_hover', false);
        
        // Construir parámetros del video
        $product_video_params = array('muted="muted"');
        
        if ( in_array('preload', $product_video_attrs) ) {
            $product_video_params[] = 'preload="auto"';
        }
        
        if ( in_array('loop', $product_video_attrs) ) {
            $product_video_params[] = 'loop="true"';
        }
        
        if ( in_array('autoplay', $product_video_attrs) ) {
            $product_video_params[] = 'autoplay="true"';
            if ( $product_video_pause_on_hover ) {
                $product_video_params[] = 'data-pause-on-hover="true"';
            }
        }
        
        // Poster optimizado
        if ( in_array('poster', $product_video_attrs) ) {
            $video_poster = '';
            if ( $product->get_image_id() ) {
                $video_poster = wp_get_attachment_image_url($product->get_image_id(), $size);
            }
            
            if ( !$video_poster && $product->get_parent_id() ) {
                $video_poster = wp_get_attachment_image_url($product->get_parent_id(), $size);
            }
            
            if ( !$video_poster ) {
                $video_poster = wc_placeholder_img_src($size);
            }
            
            if ( $video_poster ) {
                $product_video_params[] = 'poster="'.esc_url($video_poster).'"';
            }
        }
        
        // Generar HTML del video
        ob_start();
        ?>
        <video <?php echo implode(' ', $product_video_params); ?>>
            <?php if ( $video->post_mime_type == 'video/mp4' ): ?>
                <source src="<?php echo esc_url( $video->guid ); ?>" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
            <?php elseif ( $video->post_mime_type == 'video/webm' ): ?>
                <source src="<?php echo esc_url( $video->guid ); ?>" type='video/webm; codecs="vp8, vorbis"'>
            <?php elseif ( $video->post_mime_type == 'video/ogg' ): ?>
                <source src="<?php echo esc_url( $video->guid ); ?>" type='video/ogg; codecs="theora, vorbis"'>
                <?php esc_html_e( 'Video is not supporting by your browser', 'xstore' ); ?>
            <?php endif; ?>
        </video>
        <?php echo str_replace('class="', 'class="hidden ', $image); ?>
        <?php
        
        $output = ob_get_clean();
        $processed_videos[$product_id] = $output;
        return $output;
    }, 10, 3);
    
    add_filter('etheme_product_grid_list_product_element_image_wrapper_class', function($class, $product) {
        static $has_video_cache = []; // Cache estático
        
        $product_id = $product->get_id();
        
        if (isset($has_video_cache[$product_id])) {
            return $has_video_cache[$product_id] ? $class . ' product-video-as-image' : $class;
        }
        
        // Verificación optimizada
        $product_video_thumbnail = etheme_get_option('product_video_thumbnail', false);
        if ( !$product_video_thumbnail ) {
            $has_video_cache[$product_id] = false;
            return $class;
        }
        
        if ( etheme_get_custom_field('product_video_thumbnail', $product_id) == 'disable' ) {
            $has_video_cache[$product_id] = false;
            return $class;
        }
        
        // Verificar cache transiente
        $videos = get_transient('etheme_video_' . $product_id);
        if ($videos === false) {
            $videos = etheme_get_attach_video($product_id);
            set_transient('etheme_video_' . $product_id, $videos, 12 * HOUR_IN_SECONDS);
        }
        
        $has_video = !empty($videos[0]);
        $has_video_cache[$product_id] = $has_video;
        
        return $has_video ? $class . ' product-video-as-image' : $class;
    }, 10, 2);
    
    
    Avatar: Jack Richardson
    Jack Richardson
    Support staff
    January 27, 2026 at 09:55

    Hello @molo,

    Could you please provide us with more details about the specific improvements your server support managers made to the child theme code? Since we do not have access to the previous version of your file, it would be helpful for us to understand the solution they implemented. This information could also be useful for assisting other customers with similar cases. We would be grateful if you could share the details of the solution.

    You can attach the link to download files previously uploading them on services such as https://fex.net/, instead of posting all code from your file in the reply.

    Regarding the filtering issue, by default, all WooCommerce filters search for products across all stock statuses. If you would like to display only in-stock items or other specific statuses, you can add the appropriate “Product Status Filters” widget (https://prnt.sc/t83Q1EKmHwqi) to your sidebar. This will allow customers to filter products by in-stock or out-of-stock status (https://prnt.sc/IEUkJjGL23jc).

    We have added the mentioned widget to your sidebar area, so you can now check the result on the frontend.

    Best regards,
    Jack Richardson
    The 8Theme’s Team

  • Viewing 3 results - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.