Home > other >  500 Internal Server Error in PHP 8.1 Array with wordpress
500 Internal Server Error in PHP 8.1 Array with wordpress

Time:11-24

Jut updated wordpress and woocommerce to the latest versions. Unfortunately im getting a 500 Internal Server somewhere in the following lines:

add_action( 'woocommerce_before_shop_loop_item', 'attribute_img_loop', 20 );
function attribute_img_loop() {
        global $post;
        $attribute_names = array( 'pa_sertifikalar' ); // Add attribute names here and remember to add the pa_ prefix to the attribute name
        // echo '<script>alert('.$attribute_names.');</script>';

            
        foreach ( $attribute_names as $attribute_name ) {
            $taxonomy = get_taxonomy( $attribute_name );
            
            if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
                $terms = wp_get_post_terms( $post->ID, $attribute_name );
                $terms_array = array();
            
                if ( ! empty( $terms ) ) {
                    
                    echo '
                            <table  cellspacing="0">
                                <tbody>
                                        <tr>                        
                                            <td>                            
                                                <ul >';                                                  
                                                    foreach ( $terms as $term ) {
                                                        $thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
                                                        $image_id = absint( get_term_meta( $term->term_id, 'nm_pa_image_thumbnail_id', true ) );
                                                        $img_src = ( $image_id ) ? wp_get_attachment_url( $image_id ) : '';         
                                                        $archive_link = get_term_link( $term->slug, $attribute_name );
                                                        $full_line = '<li><div ><img id="c'. $image_id .'" data-src="'. $img_src .'" alt=""  alt="'. $term->name .'" /></div></li>';
                                                        array_push( $terms_array, $full_line );
                                                    }
                                                    // echo $taxonomy->labels->name . ' ' . implode( $terms_array, '-- ' );
                                                    echo implode( $terms_array, '');
                                                    
                                          echo '</ul>
                                            </td>
                                        </tr>
                                </tbody>
                            </table>';                                                  
                                                    
                }
                            
            }
        }
}

Just added echo '<script>alert('.$attribute_names.');</script>'; to check the output from the $attribute_names = array( 'pa_sertifikalar' ); part and saw it returns a

function Array() {
    [native code]
}

Any idea whats going on here? Using PHP 8.1

CodePudding user response:

From a discussion in the comments, the fix is to change implode( $terms_array, '') to implode('', $terms_array).

Historically the order of these parameters didn't matter, however as of PHP 7.4 passing the array first was deprecated and it was completely removed in PHP 8.0.

  • Related