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']) : [];
    }

So the line in question must be:

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

The upgrade always worked seamlessly, however since 5.6.2 and 5.7 something must have changed within WordPress. Since I do not have contact with the original developer any longer it would be great if someone could help me address this or change the affected code in order to have my function work again? Some expert advice would be greatly appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Not all of us have a perfect encyclopedic knowledge of the internals. It would certainly help me (and probably others) to know which files these code blocks come from.

All I can tell you at the moment is that $selected_tourstyles is probably an empty array if it is an array at all.

To debug look for where $_GET['tourstyles'] is coming from. Probably a link or a form. It could be that the link or form is not getting the data to put into the query string.

You could try wrapping:

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

with:

if(count($selected_tourstyles)>0){
    write_log('selected_original_tours: ' . $selected_tourstyles[0]);
}

This will only run that line if the array has something in it. I have no earthly idea if that will be good for the overall design of whatever this code does.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x