0

I am trying to help a small business with their Wordpress site but am unable to figure out why their site is generating this error. Here are the details:

The error is this: Undefined offset: -1 in /home/sojour15/public_html/wp-includes/post-template.php on line 278

And here's the code from the post-template.php - starting with line 275 - line 278 is "$content = $pages[$page - 1];"

if ( $page > count( $pages ) ) // if the requested page doesn't exist
    $page = count( $pages ); // give them the highest numbered page that DOES exist

$content = $pages[$page - 1];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
    $content = explode( $matches[0], $content, 2 );
    if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
        $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );

    $has_teaser = true;
} else {
    $content = array( $content );
}

I have read some about undefined offset errors and understand that this means that the code is referring to something in an array that doesn't exist but I am not a PHP coder - just someone trying to help a small business - and I'm not sure how to fix this. I tried a hack I found somewhere - just put an '@' in front of line 278. Weirdly, this hack worked for about a week. Now it's not working anymore - and it would be better to properly fix the code anyway. Any guidance would be very welcome. Thanks. Here also is a link to one of the pages where this happens: https://www.sojournacupuncture.com/treatments-and-services/

2 Answers2

0

$page is probably having 0. Thus, $pages[0-1] in the array, -1 index does not exist.

You can put a check if $page is empty or 0, then it should not execute the rest of codes. Hope this works for you.

if ( $page > count( $pages ) ) // if the requested page doesn't exist
    $page = count( $pages ); // give them the highest numbered page that DOES exist

// A check on $page
// If it is not empty, then it should execute the rest
if (!empty($page)) {
    $content = $pages[$page - 1];
    if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
        $content = explode( $matches[0], $content, 2 );
        if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
            $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );

        $has_teaser = true;
    } else {
        $content = array( $content );
    }
}
ryanicle
  • 176
  • 2
  • 5
  • Thank you so much for the quick response! OK so I tried that - but unfortunately the error didn't go away. Even weirder - the error is still referencing the same line (278) although that line now is just one of your comments. I double checked and I defnitely edited the correct file, and the code now is exactly what you just posted. I have to go to sleep now but any other thoughts would be very welcome. Thanks again for your help!! – Louise Kelly Apr 19 '16 at 04:09
  • Oh I take it back - now I'm getting two new errors instead: Undefined variable: content in /home/sojour15/public_html/wp-includes/post-template.php on line 296 Undefined variable: content in /home/sojour15/public_html/wp-includes/post-template.php on line 303 – Louise Kelly Apr 19 '16 at 04:11
  • I am going to roll back the changes we made since this new error actually makes the page look even worse. – Louise Kelly Apr 19 '16 at 04:12
  • If it is notice, you should just hide it. To hide php notice, you can follow this link http://stackoverflow.com/questions/2867057/how-do-i-turn-off-php-notices – ryanicle Apr 19 '16 at 07:26
0

Try this code.

if ( $page > count( $pages ) ) // if the requested page doesn't exist
    $page = count( $pages ); // give them the highest numbered page that DOES exist

$content = ctype_digit($page) && $page > 1 ? $pages[$page - 1] : $pages[0];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
    $content = explode( $matches[0], $content, 2 );
    if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
        $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );

    $has_teaser = true;
} else {
    $content = array( $content );
}

If it is not fine and your site is working fine then..you can hide the warning and notices by following the instructions in the bellow link

HIDE WORDPRESS WARNINGS AND NOTICES

Mohammedshafeek C S
  • 1,916
  • 2
  • 16
  • 26
  • Thank you for your help! I tested again and my hack actually IS working- I just needed to purge the server cache to get it to take effect. But I'm also going to follow the tips in the 'hide wordpress warnings' - that is super helpful. Thank you!! – Louise Kelly Apr 19 '16 at 13:23
  • And just to confirm - suppressing the error (just setting wp-debug to false) completely did the trick! – Louise Kelly Apr 19 '16 at 13:29