Possible PHP/WP problem with code, trouble accessing custom archive links

Joined
Mar 25, 2021
Messages
28
Reaction score
0
Hi, I have an old custom WP site that was working fine running PHP 7.3.9 and WP 4.9.18 and since upgrading to PHP 7.4.21 and WP 5.6.10 the archive links by year to access older data are no longer doing anything other than reloading the same page. I was wondering if someone might be able to take a look and see if they see anything obvious that could be causing the issue on the PHP side? I added a few comments of some errors WP was logging but they seem fairly inconsequential in regard to this problem but maybe there is something there. Much appreciated thanks!

PHP:
<!-- Was working PHP 7.3.9, WP 4.9.18
     Not working PHP 7.4.21, WP 5.6.10 -->
     <div>
  <h3>Newsletter</h3>
  <ul>
    <?php $urlSplits = explode('/', $_SERVER['REQUEST_URI']); ?>
    <?php $counter = 0; ?>
    <?php
      while(the_repeater_field('external-links')):
        $tempDate = explode(',', get_sub_field('date'));
        $mediaYears[] = $tempDate['1'];                   // *** Notice: Undefined offset: 1
        $data[$counter]['year'] = $tempDate['1'];         // *** Notice: Undefined offset: 1
        $data[$counter]['date'] = get_sub_field('date');
        $data[$counter]['link'] = get_sub_field('link');
        $data[$counter]['title'] = get_sub_field('title');
        $counter++;
      endwhile;
    ?>
    <?php
      $keys = 0;
      $mediaYears = array_unique($mediaYears);
      foreach ($mediaYears as $value) {
        if ($value != '') {
          $uniqueYears[$keys] = $value;
          $keys++;
        }
      }
      rsort($uniqueYears);
      $data = array_reverse($data, true);
      if ($urlSplits[3] != '') {
        $currentYear = str_replace(' ', '', $urlSplits[3]);
      } else {
        $currentYear = str_replace(' ', '', $uniqueYears[0]);
      }
    ?>
    <?php for($i = count($data); $i >= 0; $i--) { ?>
      <?php if ($currentYear == $data[$i]['year']) {
        // *** Notice: Undefined offset: 135
        // *** Notice: Trying to access array offset on value of type null
      ?>
      <li>
        <em><?php echo $data[$i]['date'] ?></em>
        <strong><a href="<?php  echo $data[$i]['link'] ?>" rel="bookmark"><?php  echo $data[$i]['title'] ?></a></strong>
      </li>
      <?php } ?>
    <?php } ?>
  </ul>
  <?php for ($i = 0; $i < count($uniqueYears); $i++) { ?>
    <!-- THIS IS THE LINE THAT CREATES THE YEAR LINKS TO GO TO ARCHIVE PAGES -->
    <a href="https://www.domain.org/news/newsletter/<?php  echo str_replace(' ', '', $uniqueYears[$i]); ?>"><?php  echo $uniqueYears[$i]; ?></a>
    <!-- Permalink set to Custom Structure: /news/%category%/%year%/%monthnum%/%postname%/ (Not sure if this is relevant) -->
  <?php } ?>
</div>
 
Joined
Jan 8, 2023
Messages
27
Reaction score
2
It looks like the issue may be with the way that the page is trying to access the $_SERVER['REQUEST_URI'] variable.

The $_SERVER['REQUEST_URI'] variable returns the URI that was given in order to access this page, and it looks like the code is trying to split the URI by '/' characters and access the third element in the resulting array.

However, it is possible that the URI may not always have enough '/' characters, in which case accessing the third element of the array would cause an error.

One solution to this issue could be to check if the third element of the array exists before trying to access it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top