2

I have checked the other related posts but unfortunately it didn't help my issue.

I am getting

Warning: Use of undefined constant _ - assumed '_' (this will throw an Error in a future version of PHP)

error after updating to PHP version 7.2

I traced the reason to this code snippet:

<span class="post-container__excerpt-price"><?php echo '$' . number_format( (float)get_field('price', $post->ID) );  ?></span>

When I remove this the error goes away but I can't seem to find any glaring issues with this code either. 'price', $post->ID is referring to a custom field that is created with ACF.

Anyone has any idea? Thanks a lot!

The entire code block is below:

// create shortcode to list all listings
add_shortcode( 'list-posts-basic', 'rmcc_post_listing_shortcode1' );
function rmcc_post_listing_shortcode1( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'listings',
        'posts_per_page' => -1,
        'order' => 'DESC',
        'orderby' => 'date',
    ) );
    if ( $query->have_posts() ) { ?>
        <div class="posts-container">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <div class="post-container" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <a class="" href="<?php the_permalink(); ?>"><div class="listing-post-img" style="background: url(<?php echo get_the_post_thumbnail_url() ?> )"></div></a>

                <div class="post-container__content">
                    <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
                    <p class="post-container__excerpt">
                        <?php the_excerpt();  ?>
                        <span class="post-container__excerpt-price"><?php echo '$' . number_format( (float)get_field('price', $post->ID) );  ?></span>
                    </p>

                    <a class="post-container__button" href="<?php the_permalink(); ?>">View Details</a>
                </div>
            </div>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </div>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}
user3783243
  • 5,368
  • 5
  • 22
  • 41
TheSilentMan
  • 21
  • 1
  • 1
  • 5
  • Is `$post->ID` supposed to be the woocommerce product id? – Howard E Feb 26 '20 at 18:09
  • It is part of ACF (Advanced Custom Fields) – TheSilentMan Feb 26 '20 at 18:21
  • what happens if you put `number_format(floatval(get_field('price', $post->ID)), 2)` – Howard E Feb 26 '20 at 18:51
  • That did not work... – TheSilentMan Feb 26 '20 at 19:12
  • Are you 100% sure that's the code causing the warning, exactly as it is? Because there's no out-of-place underscore there. – Aioros Feb 26 '20 at 19:13
  • If you use the `query monitor` plugin https://wordpress.org/plugins/query-monitor/, it gives you line numbers for the errors in detail. – Howard E Feb 26 '20 at 19:22
  • The error message specifically referred to that line of code and when I removed that, the error went away. I copied the entire code bock above – TheSilentMan Feb 26 '20 at 19:24
  • @TheSilentMan, then I suggest trying to dig more. Which part of that line is the problem? Does it work if you put a literal (numeric) string instead of `get_field()`? Does it work if you put an actual `float` instead of the cast? Does it work if you remove `number_format()` altogether? – Aioros Feb 26 '20 at 19:38
  • Try this... Change to `number_format( (float)get_field('price', $query->ID) )` – Howard E Feb 26 '20 at 19:42
  • Can you show the contents of the `get_field()` function? Since there is no single `_` I presume the error happens in there. – ArSeN Feb 26 '20 at 20:14

3 Answers3

0

The issue is $post->ID. Global $post is not accessible at that point.

You would need to add global $post; or you could swap get_the_ID() in its place.

As well, you can shorten this.

<?php $myvariable = ob_get_clean();
    return $myvariable;
    }

Short version as there is no reason to just declare a variable just to return.

<?php return ob_get_clean();
}

Testing with $post->ID enter image description here

Bazdin
  • 1,049
  • 7
  • 9
  • How would I do that? Could you show the corrected code? – TheSilentMan Feb 27 '20 at 14:46
  • or before ob_start(); add $global $post; – Bazdin Feb 27 '20 at 14:55
  • Unfortunately that didn't work, still getting the error when I change the PHP version 7.3 – TheSilentMan Feb 27 '20 at 15:03
  • did what? I listed two ways. – Bazdin Feb 27 '20 at 15:04
  • Tried both with the same result :/ – TheSilentMan Feb 27 '20 at 15:22
  • If your host has some type of caching that would need to be purged. Using your supplied shortcode with error reporting on kicks out with the $post->ID. I've updated it with an image with me enabling all debugging and causing it to kick out. – Bazdin Feb 27 '20 at 15:25
  • This is also running on PHP 7.3.5 – Bazdin Feb 27 '20 at 15:29
  • I purged the server cache and still same result. when I define $post; and $global; and swap the $post->ID with get_the_ID() I still get the error but only on the first rendered HTML block of the loop as opposed on every single one of them. Very strange behaviour – TheSilentMan Feb 27 '20 at 15:57
  • hmm What version of ACF are you running? – Bazdin Feb 27 '20 at 16:43
  • ACF version I am using is 5.8.7 – TheSilentMan Feb 27 '20 at 16:54
  • Are you able to share any additional code on that page? I'm running ACF Pro 5.8.7 and outside of $global post; not being called directly in the shortcode There is nothing there that would cause that. get_field() actually has access to it. – Bazdin Feb 27 '20 at 17:13
  • Aside from the shortcode that I created with that PHP block (copied up in the first message) there is nothing else on the page itself. I am just calling the function using the [list-posts-basic] shortcode to render all the inventory. – TheSilentMan Feb 27 '20 at 20:00
  • As soon as I update from PHP 7.1.33 to anything higher it becomes like this: https://www.dropbox.com/s/t8nryun1an9znal/Screen%20Shot%202020-02-27%20at%2012.01.52%20PM.png?dl=0 – TheSilentMan Feb 27 '20 at 20:03
  • Are you able to share the contents of your functions file? I wonder if there is a issue somewhere else and its just getting displayed there. – Bazdin Mar 05 '20 at 22:53
0

You have to insert just this code:

return ob_get_clean();

If your code is:

.....
...
..

Do:

return ob_get_clean();

It works fine for me.

Soroush Chehresa
  • 5,490
  • 1
  • 14
  • 29
0

I think I found the problem causing this exact error undefined constant _ - assumed '_'. PHP 7 seems to be very particular about whitespace before or after the <?php and the ?>. The reason it's referring to an "undefined constant" _ is because the character _ is probably a Unicode character. In my case it was Unicode for a non-breaking space, and it happened to be right before the ?>.

In your code, you said it was on this line - notice the two spaces just before the ?>:

<span ...><?php echo ...get_field('price', $post->ID) );  ?></span>

I'd bet that the second space is actually a Unicode character, and that's what's causing the warning. In my case, overwriting it with an actual space fixed the problem, even though it appeared exactly the same. I was able to confirm that mine was a Unicode character by opening it in a hex editor, which displayed the "space" as 0xC2A0, and not the usual 0x20. I checked yours as well, but my guess is that it got translated to an actual space when pasting it into this web site.

More info:

What is “=C2=A0” in MIME encoded, quoted-printable text?

Warning: Cannot modify header information - headers already sent

Russell G
  • 470
  • 6
  • 16