1

I created a ACF block. It calculates the distance from the displayed ski resort to the 3 closest ski resorts and displays a summary and is meant to use in a side bar or something.

I have a function called distance() that takes two latitude and longitude points and calculates the difference. I am getting a PHP Fatal Error when I edit the custom post type fs_ski_resorts, although in the front end everything displays correctly. Also, I have added this block to 2 custom post types. They both display perfectly, however, 1 of the 2 produces the fatal error in the backend with editing it. There is only 1 block on the page, too. Strange.

[25-Aug-2024 12:09:31 UTC] PHP Fatal error:  Cannot redeclare distance() (previously declared in /home/example/htdocs/www.example.com/wp-content/plugins/fs-nearby-resorts-block/template-parts/fs-nearby-resorts-block-template.php:101) in /home/example/htdocs/www.example.com/wp-content/plugins/fs-nearby-resorts-block/template-parts/fs-nearby-resorts-block-template.php on line 101

I have only declared distance one, however, it seems somehow it is getting declared twice. I cannot figure out how to debug this anymore... any ideas on how to debug or what the problem may be?

Here is the code:

<?php    
$id = 'fs-nearby-resorts-block-'.$block['id'];
if(!empty($block['anchor'])){
    $id =  $block['anchor']; 
}

if(!empty($block['className'])){
    $className .= ' '.$block['className']; 
}

//if(!empty($block['align'])){
//    $className .= ' align'.$block['align']; 
//}

$className = "fs-nearby-resorts-block";

global $post;
$post_slug  = $post->post_name;

$distanceFromSkiResort  = 100; // in kilometers.

$latitude_from              = do_shortcode('[fs_ski_resort_field key="aa_y" id="'.$post_id.'"]');
$longitude_from             = do_shortcode('[fs_ski_resort_field key="aa_x" id="'.$post_id.'"]');

// args
$args = array(
    'posts_per_page'    =>-1,
    'post_type'         => 'fs_ski_resorts',
);

// query
$the_query = new WP_Query( $args );

if( $the_query->have_posts() ): ?>

<ul>
    <?php 
        $resortArray = array(); 
        
        while( $the_query->have_posts() ) : $the_query->the_post(); 
        
            $to_ski_resort_id = get_the_ID();
            
            $latitude_to = do_shortcode('[fs_ski_resort_field key="aa_y" id="'.$to_ski_resort_id.'"]');
            $longitude_to = do_shortcode('[fs_ski_resort_field key="aa_x" id="'.$to_ski_resort_id.'"]');
            $distance = (float) round(distance($latitude_from, $longitude_from, $latitude_to, $longitude_to, 'K'),1);
            
            if ($distance <= $distanceFromSkiResort) {
                
                if ($post_id <> $to_ski_resort_id) { // remove the current resort from the list of ski resorts within X km.
                    
                    $resortData =  array (
                        'get_permalink'             => get_permalink(),
                        'get_the_title'             => get_the_title(),
                        'distanceToResort'          => $distance,
                        'aa_pistes_length_km'       => do_shortcode('[fs_ski_resort_field key="aa_pistes_length_km" id="'.$post_id.'"]'),
                        'aa_lifts_length_km'        => do_shortcode('[fs_ski_resort_field key="aa_lifts_length_km" id="'.$post_id.'"]'),
                        'aa_pistes_count'           => do_shortcode('[fs_ski_resort_field key="aa_pistes_count" id="'.$post_id.'"]'),
                        'aa_lifts_count'            => do_shortcode('[fs_ski_resort_field key="aa_lifts_count" id="'.$post_id.'"]'),
                        'aa_min'                    => do_shortcode('[fs_ski_resort_field key="aa_min" id="'.$post_id.'"]'),
                        'aa_max'                    => do_shortcode('[fs_ski_resort_field key="aa_max" id="'.$post_id.'"]'),
                        'aa_ttl_vertical'           => do_shortcode('[fs_ski_resort_field key="aa_ttl_vertical" id="'.$post_id.'"]'),
                        'aa_novice_easy_length_km'  => do_shortcode('[fs_ski_resort_field key="aa_novice_easy_length_km" id="'.$post_id.'"]'),
                        'aa_novice_easy_count'      => do_shortcode('[fs_ski_resort_field key="aa_novice_easy_count" id="'.$post_id.'"]'),
                        'aa_intermediate_count'     => do_shortcode('[fs_ski_resort_field key="aa_intermediate_count" id="'.$post_id.'"]'),
                        'aa_intermediate_length_km' => do_shortcode('[fs_ski_resort_field key="aa_intermediate_length_km" id="'.$post_id.'"]'),
                        'aa_advanced_expert_freeridee_extreme_count'        => do_shortcode('[fs_ski_resort_field key="aa_advanced_expert_freeridee_extreme_count" id="'.$post_id.'"]'),
                        'aa_advanced_expert_freeridee_extreme_length_km'    => do_shortcode('[fs_ski_resort_field key="aa_advanced_expert_freeridee_extreme_length_km" id="'.$post_id.'"]'),
                    );
    
                    $resortArray[get_the_ID()] = $resortData;
                }
           } 
           
        endwhile; 
        
        array_multisort(array_column($resortArray, 'distanceToResort'), SORT_ASC, $resortArray);

        $i = 1;
        
        echo '<h2>3 Ski Resorts Nearby: </h2>';
        foreach ($resortArray as $resort) {
            echo '<a href="'.$resort['get_permalink'].'">'. $resort['get_the_title'] .'</a> is '. $resort['distanceToResort']  . "km away!</br>";
            echo '<ul>';
            echo '<li style="color:#339933"> Beginner: '.$resort['aa_novice_easy_count'].' runs over '.$resort['aa_novice_easy_length_km'].' km </li></br>';
            echo '<li style="color:#ed151f"> Intermediate: '.$resort['aa_intermediate_count'].' runs over '.$resort['aa_intermediate_length_km'].' km </li></br>';
            echo '<li>Expert: '.$resort['aa_advanced_expert_freeridee_extreme_count'].' runs over '.$resort['aa_advanced_expert_freeridee_extreme_length_km'].' km </li></br>';
            echo '</ul>';
            echo '<hr></br>';
            if ($i++ == 3) break;
        }
    ?>
</ul>

<?php endif; ?>

<?php wp_reset_query();   // Restore global post data stomped by the_post()

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);
    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}
3
  • 2
    It looks like you're defining distance() in a template file. Try moving that code to a separate file that is then loaded by the template file (using, eg, require or something similar).
    – Pat J
    Commented Aug 25 at 14:28
  • 2
    To elaborate on @PatJ's point, a template file may be included multiple times. Thus, if there is a function declaration in one, that would be included multiple times too. Thus, this causes the redeclaration error.
    – Wongjn
    Commented Aug 25 at 22:01
  • 1
    Thank you very much. I created a function.php and called it from my template with require_once('functions.php'); and it now works. If @Pat J sets the answer, I can check it as correct. Much appreciated! Also thank you to Wongjn ! Commented Aug 26 at 3:18

1 Answer 1

2

It looks like you're defining distance() in a template file. As @wongjin pointed out in the comments, a template file may be included multiple times, and so on the second inclusion PHP will try to redefine distance(), resulting in a fatal error.

Try moving the function distance(){ ... } code to a separate file that is then loaded by the template file (using, eg, require or something similar).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.