skip to Main Content

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;
    }
}

3

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search