Home Forums Backend Issues (wp-admin) Some PHP function calls aren't shown in the backend preview


Some PHP function calls aren't shown in the backend preview

  • Why doesn’t some PHP function call shown in the backend preview? (Some do)

    I then noticed that while some PHP function works on the front-end and show proper results, but in the preview of the block, some doesn’t show the value that was returned from the function call. For example:

    <?php echo get_the_title(); ?>
    <?php echo get_the_post_thumbnail_url( null, 'full' ); ?>

    These PHP calls doesn’t show anything in preview. But works in frontend.

    But when I use another PHP to echo the custom taxonomy it is shown in the preview properly. The code is given below:
    <?php foreach((get_terms('character_rarity')) as $terms) { echo $terms->name; }; ?>

    So why is this happening? My main goal of creating the post is to know that. It’s not technically creating much of a trouble. As it is working in the front end as it was supposed to. But it is kind of decreasing the efficiency for the purpose of the preview option. So mainly would like to know why it’s happening and if it is possible to avoid it somehow.

  • Answered here.

    Please keep in mind that some WP functions such as get_the_title() and get_the_post_thumbnail_url() refer to a global $post object if the $post_id variable is not provided (or in your case set to null). During the AJAX request to render the block preview, that global $post object is not setup, which explains why those functions are not running as intended.

    The soution is simple, either setup the gobal $post object manually, or use the provided $post_id parameter in any get_the_title() or get_the_post_thumbnail_url() calls 👍.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.