Title: 4 PHP errors to be corrected
Last modified: October 27, 2024

---

# 4 PHP errors to be corrected

 *  [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/)
 * I got these errors
 * NoticeTrying to access array offset on value of type bool+
    1. wp-content/themes/sacconicase/functions.php:2543
 * 1TemaNoticeTrying to get property ‘term_id’ of non-object+
    1. wp-content/themes/sacconicase/functions.php:2543
 * 1TemaNoticeUndefined index:+
    1. wp-content/themes/sacconicase/functions.php:2543
 * 1Tema
 * these errors all at the same line of the code `$terms_destinazione = $names_trans[
   get_the_terms( $post->ID, 'tipologia') [0]->term_id ] ;`
 * the complete code is here [https://pastebin.com/bE190JeF](https://pastebin.com/bE190JeF)
 * And I got a fourth error:
 * Notice (Soppresso)unserialize(): Error at offset 2 of 428 bytes+
    1. wp-includes/functions.php:650
 * 1Plugin: redirect-redirection
 * It seems it depends from a plug-in? it’s not a custom plug-in, what can I do?
    -  This topic was modified 1 year, 5 months ago by [sacconi](https://wordpress.org/support/users/sacconi/).
    -  This topic was modified 1 year, 5 months ago by [sacconi](https://wordpress.org/support/users/sacconi/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2F4-php-errors-to-be-corrected%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 15 replies - 1 through 15 (of 15 total)

 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18098403)
 * ok, I study this code, but I realize that this error should have never happened
   because every post must have a “tipologia” term assigned. So I found the incomplete
   post and solutioned this kind of error. I think I’ll open another topic to get
   help in building a check in case I forget to choose a term of that taxonomy, 
   that should be important
 * In the meanwhile I found 2 more php errors in the single post page:
 * NoticeUndefined variable: user+
    1. wp-content/plugins/mysite-plugin/mysite-plugin.php:20
 * 1Plugin: mysite-pluginNoticeTrying to get property ‘ID’ of non-object+
    1. wp-content/plugins/mysite-plugin/mysite-plugin.php:20
 * the problem is inside here:
 *     ```wp-block-code
       $current_year =  get_the_author_meta( 'current_year', $user->ID );// Translators: %s: author's current year.$titolo_display_price = sprintf(    '<div class="titolo_listino_prezzi" ID="titolo_listino_prezzi">' .    '<h3 class="titolo_affitti">' . esc_html( sprintf( __('Rates %s in &euro;/ week:','mysite-plugin'), $current_year ) ) . '</h3>' .    '</div>',    esc_html( $current_year ));
       ```
   
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18098539)
 * I tryed to correct, is it right? [https://pastebin.com/qJP0nxuT](https://pastebin.com/qJP0nxuT)
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18098640)
 * it’s correct but not what I need…new expriment: [https://pastebin.com/7ZpcrhUm](https://pastebin.com/7ZpcrhUm)
 * I STILL have PHP errors
    -  This reply was modified 1 year, 5 months ago by [sacconi](https://wordpress.org/support/users/sacconi/).
    -  This reply was modified 1 year, 5 months ago by [sacconi](https://wordpress.org/support/users/sacconi/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18098727)
 * The same errors as before I assume. “Trying to get property ‘ID’ of non-object”
   is a knock-on effect from “Undefined variable: user”. Correct the undefined user
   problem and the other will go away.
 * You may need to assign a WP_User object to $user. What data is the user coming
   from? Post author? If so, and you only need the author’s ID, if $post is global,
   the author’s ID is `$post->post_author`. You then do not need `$user->ID`.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18099487)
 * this code breacks the site:
 *     ```wp-block-code
       $terms = get_the_terms($post->ID, ‘tipologia’);if (isset($names_trans[$terms[0]->term_id])) {$terms_destinazione = $names_trans[$terms[0]->term_id];} else {$terms_destinazione = 'null'; // or handle it accordingly}
       ```
   
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18099528)
 * About the other code , ok, I put `$post->post_author` instead of `$user->ID` 
   and I have no longer the errors, but it is correct my solution about the “else”?
   [https://pastebin.com/nVT3V6Ud](https://pastebin.com/nVT3V6Ud) . When the meta
   field in post editor is empty I still need a div with some text inside is printed,
   only the year is not visible
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18100744)
 * The else condition is OK, but it’s actually unnecessary. get_the_author_meta()
   returns an empty string when a meta value for the current post does not exist.
   Even without the else condition, the output is `Rates in €/ week:` without a 
   meta value. There are two spaces after “Rates” though, whether you use the else
   condition or not. The way to correct that (if you want) is to remove one of the
   spaces in the translation string and if post meta is not empty, then append a
   space to $current_year. There’s no need to redo the entire HTML just for one 
   space.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18101734)
 * Ok, so I just use the code without conditional? I tested it and I dont have indefinite
   variable errors
 * CODE 1
 *     ```wp-block-code
       $current_year =  get_the_author_meta( 'current_year', $post->post_author );// Translators: %s: author's current year.$titolo_display_price = sprintf(    '<div class="titolo_listino_prezzi" ID="titolo_listino_prezzi">' .    '<h3 class="titolo_affitti">' . esc_html( sprintf( __('Rates %s in &euro;/ week:','mysite-plugin'), $current_year ) ) . '</h3>' .    '</div>',    esc_html( $current_year ));
       ```
   
 * and what about this code breacking the site?
 * CODE 2
 *     ```wp-block-code
       $terms = get_the_terms($post->ID, ‘tipologia’);if (isset($names_trans[$terms[0]->term_id])) {$terms_destinazione = $names_trans[$terms[0]->term_id];} else {$terms_destinazione = 'null'; // or handle it accordingly}
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18102882)
 * Code 1 is OK because $current_year will always have a useful value, even if it’s
   an empty string.
 * Code 2: The quotes for `‘tipologia’` are the wrong form, should be `'tipologia'`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18103108)
 * Code 2: I still get 2 PHP errors
 * NoticeTrying to access array offset on value of type bool+
    1. wp-content/themes/sacconicase/functions.php:2549
 * 1TemaNoticeTrying to get property ‘term_id’ of non-object+
    1. wp-content/themes/sacconicase/functions.php:2549
 * 1Tema
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/#post-18105773)
 * Apparently $terms value is `false`, a boolean value. This is either because the
   post has no terms assigned to it, or there’s no such post referenced by $post.
   Are you sure the current post has tiplogia terms assigned to it? If so, $post
   may be out of scope. Try using `get_the_ID()` instead of `$post->ID`.
 * If there’s the slightest chance now or in the future that a post may not have
   have any tipologia terms assigned to it, checking with `isset()` is not enough,
   you should first check if $terms is an array before checking with isset().
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/page/2/#post-18106117)
 * Theoretically every apartment (post) should have a “tipologia” term assigned 
   but my problem is that sometimes I forget to select a tipologia. If I remember
   well you told me that it’s not possible to set as mandatory the selection of 
   a custom taxonomy term, maybe the smartest and unique solution is to set a js
   pop up message warning me of my forgetting. In any case what I need is a code
   that provides for the possibility that I forget to select a tipologia term. I
   dont know what is better, maybe a simple message of warning? I should warm myself
   of many fields. Sometimes I forget to set the agency (author), and this too is
   a problem…
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/page/2/#post-18109959)
 * I don’t think I said it’s not possible. More likely that’s it’s difficult to 
   accomplish on the editor screen. It should be feasible with the right JS code.
 * In any case, it could happen there are no assigned terms, even though it’d be
   unintentional. Thus your code needs to accommodate for this possibility. Verify
   with `is_array( $terms )`. I think this could replace the `isset($names_trans[
   $terms[0]->term_id]` check, but to be extra safe you could do both by logically
   ANDing the two with `&&`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/page/2/#post-18110694)
 * I did this and the PHP errors left have disappeared
 *     ```wp-block-code
       $terms = get_the_terms($post->ID, 'tipologia');if (is_array( $terms ) && isset($names_trans[$terms[0]->term_id])) {$terms_destinazione = $names_trans[$terms[0]->term_id];} else {$terms_destinazione = 'null'; // or handle it accordingly}
       ```
   
 * a double check can influence the speed of the execution of the code? I suppose
   it doesnt
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/page/2/#post-18111126)
 * Technically speaking it takes some extra time, but it’s negligible. Perhaps several
   microseconds.

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘4 PHP errors to be corrected’ is closed to new replies.

## Tags

 * [PHP errors](https://wordpress.org/support/topic-tag/php-errors/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 19 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [1 year, 5 months ago](https://wordpress.org/support/topic/4-php-errors-to-be-corrected/page/2/#post-18111126)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
