0

Hi i use a widget with php code

on php7.4 everything ok

with php 8 i have an error....

Warning: Undefined variable $post in /xxxxxxx/wp-content/plugins/php-code-widget/execphp.php(27) : eval()'d code on line 2

Warning: Attempt to read property "ID" on null in /xxxxxxx/wp-content/plugins/php-code-widget/execphp.php(27) : eval()'d code on line 2

Warning: Undefined variable $post in /xxxxxxx/wp-content/plugins/php-code-widget/execphp.php(27) : eval()'d code on line 3

Warning: Attempt to read property "ID" on null in /xxxxxxx/wp-content/plugins/php-code-widget/execphp.php(27) : eval()'d code on line 3

My code:

<?php
if (get_the_terms($post->ID, 'ausstattung')) {
  $taxonomy_ar = get_the_terms($post->ID, 'ausstattung');
echo "";
  $output = '<ul>';
  foreach ($taxonomy_ar as $taxonomy_term) {
    $output .= '<li>'. $taxonomy_term->name .'</li>';
  }
  $output .= '</ul>';

  echo $output;
}
?>

What is wrong?

1
  • 1
    Someone else posted a similar issue a while back, so maybe this is a 7.4->8 issue, but I'd be surprised. (They never followed up with a resolution though.) How is that PHP file being loaded? Is it definitely run from within WordPress, at a point when the post is loaded? You might need to explicitly global $post;
    – Rup
    Commented Jan 7, 2023 at 13:50

1 Answer 1

1

You are using a global variable called $post in your code, but this variable is not defined.

To fix this issue, you can try defining the $post variable at the top of your script:

 <?php
    global $post;
    
    if (get_the_terms($post->ID, 'ausstattung')) {
      $taxonomy_ar = get_the_terms($post->ID, 'ausstattung');
    echo "";
      $output = '<ul>';
      foreach ($taxonomy_ar as $taxonomy_term) {
        $output .= '<li>'. $taxonomy_term->name .'</li>';
      }
      $output .= '</ul>';
    
      echo $output;
    }
    ?>

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.