David
David

Reputation: 43

Undefined offset: 0 in WordPress after latest version upgrade

My WordPress powered website always worked great, however after the latest upgrade to 5.6.2 and 5.7 I have a major issue that actually breaks an important part of the website, I have seen to go through the logs and found the following error:

    [14-Mar-2021 00:29:32 UTC] PHP Notice:  Undefined offset: 0 in /var/www/html/wp-content/plugins/tours/includes/class-tours-tour-post-type.php on line 1709
    [14-Mar-2021 00:29:32 UTC] selected_original_tours: 

The code in question seems to be the following:

    if($show_filter) {
        $selected_destinations = isset($_GET['destinations']) ? explode(',', $_GET['destinations']) : [];
        $selected_tourstyles = isset($_GET['tourstyles']) ? explode(',', $_GET['tourstyles']) : [];
        write_log('selected_original_tours: ' . $selected_tourstyles[0]);
        for($i = 0; $i < count($selected_tourstyles); $i++) {
            $selected_tourstyles[$i] = str_replace('and', '&', $selected_tourstyles[$i]);
            $selected_tourstyles[$i] = str_replace('_', ' ', $selected_tourstyles[$i]);
            write_log('Selected_tourstyles:' . $selected_tourstyles[$i]);
        }
        
        $selected_dates = isset($_GET['dates']) ? explode(',', $_GET['dates']) : [];
    }

The line in question will be this one:

    write_log('selected_original_tours: ' . $selected_tourstyles[0]);

How could I address this or change the affected code in order to have my function work again? Some expert advice would be greatly appreciated.

Upvotes: 0

Views: 50

Answers (1)

JohnFF
JohnFF

Reputation: 741

I'd move the write_log inside the for statement and replace the 0 with an i, so it becomes:

// write_log('selected_original_tours: ' . $selected_tourstyles[0]); < delete this line for($i = 0; $i < count($selected_tourstyles); $i++) { write_log('selected_original_tours: ' . $selected_tourstyles[$i]); // < new entry. $selected_tourstyles[$i] = str_replace('and', '&', $selected_tourstyles[$i]); $selected_tourstyles[$i] = str_replace('_', ' ', $selected_tourstyles[$i]); write_log('Selected_tourstyles:' . $selected_tourstyles[$i]); }

Then if it doesn't exist, no error.

Though I find the writing of the logs odd to be honest. Why would you need to log which was selected?

Upvotes: 1

Related Questions