• 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

    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.
    • This topic was modified 1 year, 5 months ago by sacconi.

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Thread Starter sacconi

    (@sacconi)

    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:

    $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

    (@sacconi)

    I tryed to correct, is it right? https://pastebin.com/qJP0nxuT

    Thread Starter sacconi

    (@sacconi)

    it’s correct but not what I need…new expriment: https://pastebin.com/7ZpcrhUm

    I STILL have PHP errors

    • This reply was modified 1 year, 5 months ago by sacconi.
    • This reply was modified 1 year, 5 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    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

    (@sacconi)

    this code breacks the site:

    $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

    (@sacconi)

    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 . 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

    (@bcworkz)

    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

    (@sacconi)

    Ok, so I just use the code without conditional? I tested it and I dont have indefinite variable errors

    CODE 1

    $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

    $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

    (@bcworkz)

    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

    (@sacconi)

    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

    (@bcworkz)

    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

    (@sacconi)

    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

    (@bcworkz)

    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

    (@sacconi)

    I did this and the PHP errors left have disappeared

    $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

    (@bcworkz)

    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.