0

I have written a small function that displays a Custom Post Type called Testimonials (in my case) using a shortcode.

The Shortcode runs properly. But I noticed 1 major problem with that.

My WordPress Media library doesn't show up/loads once the Shortcode function is called.

As soon as I remove the Shortcode Function it works.

I tried WP_DEBUG to see if there is an error but it does not show anything.

Also, checked php_error log, apache_error log, there is nothing that can help me.

I then went to Site Health option and there I see

"The REST API did not behave correctly" "The REST API did not process the context query parameter correctly"

Here is the custom shortcode function -

<!-- Shortcode display testimonials-->
<?php

function display_testimonials()
{
    ?><div class="owl-carousel owl-theme">
    <?php
    $args = array(
        'post_type' => 'testimonials',
        'post_status' => 'published',

    );
    $testimonials = new WP_Query($args);

    if($testimonials->have_posts()):while($testimonials->have_posts()):$testimonials->the_post();
    ?>

    <div class="item testimonial_box">
    <div class="testimonial_1box">
        <span class="testimonial_stars"><?php $stars = get_field('stars'); echo $stars;?></span>

        <span class="testimonial_content"><?php the_content();?></span>
        <h4 class="testimonial_title"><?php the_title();?></h4>

    </div>

        <div class="testimonial_2box">
            <figure class="figure_testimonial_image">
                <img class="testimonial_image" src="<?php the_post_thumbnail();?>
            </figure>
        </div>
    </div>



    <?php endwhile; else: ?>

<p>Sorry, there are no posts to display Lavede</p>

<?php endif;?>


</div>
<?php

wp_reset_postdata();


}

function testimonial_shortcode()
{
    add_shortcode('TestimonialITW', 'display_testimonials');
}

add_action('init', 'testimonial_shortcode');
2
  • Shortcodes should always return the output and not echo it. – Sally CJ Mar 2 '20 at 8:20
  • Hi. Do you mean that instead of echo $stars; I should replace it with a return keyword? i.e return $stars? I did that but then the rest of the code stops working. And also the library not loading problem still exists. Here - snipboard.io/uLdGBE.jpg I apologize for sounding silly but I am just getting started. – Chipsy Mar 2 '20 at 10:36
0

First, I'm assuming you put the code in the theme functions.php file, and if so, then there should be no direct output echoed in that file:

  • Good:

    <?php // Assume this is wp-content/themes/your-theme/functions.php
    function some_func() {
        echo 'This is a good output; it\'s not a direct output coming from the file.';
        echo 'The output is only displayed when you call the function - some_func().';
    }
    
  • Bad:

    <!-- This is a HTML comment and it's a sample direct output. -->
    <?php // Assume this is wp-content/themes/your-theme/functions.php
    function some_func() {
        echo 'This is a good output; it\'s not a direct output coming from the file.';
        echo 'The output is only displayed when you call the function - some_func().';
    }
    

Secondly, the Site Health performs a test to check if the REST API is accessible (or working good) and if it's not, which could be because of invalid response, then you'd see the error "The REST API did not behave correctly". And that's most likely what's happening in your case because you got that <!-- Shortcode display testimonials--> at the top of the file (i.e. your functions.php file). So remove that comment and the error should be gone.

Apart from that, another issue in your code is that you need to make sure your shortcode is returning the output and not echoing it. If you echo it, then the output would be displayed at the wrong place (i.e. not where you put the [TestimonialITW] in the post content) and when you update a post having the shortcode, you'd likely see an error that says "Updating failed. Error message: The response is not a valid JSON response." — because the response contains the output from your shortcode (e.g. HTML tags like the <div class="owl-carousel owl-theme">).

But if you need to echo it, you can use output buffering:

function testimonial_shortcode()
{
    ob_start();             // turn on output buffering
    display_testimonials(); // put the output to the buffer
    return ob_get_clean();  // capture and return the buffer
}
add_shortcode( 'TestimonialITW', 'testimonial_shortcode' );

And that would replace the following:

function testimonial_shortcode()
{
    add_shortcode('TestimonialITW', 'display_testimonials');
}

add_action('init', 'testimonial_shortcode');

Additionally, unless you have registered a custom post status named published, that 'post_status' => 'published' should be 'post_status' => 'publish'.

4
  • 1
    Thank you so very much for the clear explanation and writing a detailed solution for me. It worked. I learned 2 important things from the answer. Thanks again! – Chipsy Mar 2 '20 at 11:59
  • You're very welcome! And remember, the "direct output" could also be a space, so make sure it doesn't exist at the top (before the opening <?php tag) or at the end of the file (hint: don't close the PHP tag - <?php /* code here..... */ - no ?> at the end). :) Also for simple shortcodes, you could use string concatenation: $output = ''; $output .= 'append output'; return $output - but that's of course a simplified example. – Sally CJ Mar 2 '20 at 12:12
  • This will surely help me in improving the overall code. I am planning to create a FREE plugin out of this that might be helpful for others. So yeah it's a long process. Have to learn so many things before actually having a plugin of my own in the WordPress repository. Individuals like you will always help I am certain of that. – Chipsy Mar 2 '20 at 12:28
  • Yep, you can always post another question, but remember to be on-topic.. :) Anyway, good luck with that plugin! – Sally CJ Mar 2 '20 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.