Calling plugins API within a theme

I will try to keep this short and simple as possible.

My website uses listings (instead of posts) because I have a directory page theme (MyListing – this one). I review restaurants and add reviews through “Let’s Review” plugin (API documentation is here). When I review a listing the plugin adds a review box in the listing, showing pros and cons and the final score. It looks something like this:

enter image description here

What I would like is to get that final score (7.8 in this case) in raw form, without design and put it in the listing template on the top above the content (I will put it into the listing template because every listing will have a review and this box). I don’t need help where in the listing template to add it, I will do that myself once I find out how to call this API code.

The ‘Let’s Review’ theme does have API support and I did find the part of code which (I think) should suit me:

if ( class_exists( 'Lets_Review_API' ) ) {
    $lets_review_api = new Lets_Review_API();
    $lets_review_final_score = $lets_review_api->lets_review_get_final_score( $postid );
}

If I pressume correctly, this snippet calls the post ID and shows the final score in raw form. That’s what I want! But here is the problem. As I understood the part “Ensure you are calling it from inside the WordPress post loop” it means I have to add this code to functions.php of the theme. But everything else is beyond me. I don’t know how to call this final score in the raw form in my listing template? The documentation also states “You must pass a valid post id to the method to get the correct data” but I don’t know how.

Did I do the right thing to add this code to functions.php? And which code to add to my listing template (listing.php) so it will, as I pressume, fetch the post ID and show the raw score of that review.

Any help (or taking me into the right direction) is appreciated.

1 Answer
1

Damn it, I even figured it out and it works.

So, I added this directly into my listing template, nothing was added into functions.php nor was any other file altered. I first called the $post variable – in the WordPress documentation it says it “contains data from the current post in The Loop.” I figured this will work as the code has to be in the loop and added it to the top.

As said in the documentation “You must pass a valid post id to the method to get the correct data” I thought to myself well, I somehow need to get that post ID before everything is executed. Therefore the $id string, which gets the WordPress post ID.

The second part, from if ( class... to the last part $id ); is from the plugin API documentation, and it calls only the final score of the review, without any design.

But heck, this didn’t show the review yet. I heard once “echo” does miracles, as it calls out the function and it shows it. So I said – why not? By WordPress documentation echo outputs one or more strings.

And voilá – it works. I learned a lot of new things by combining different sources and it took me 3 days to figure everything out. Now it’s great I know how to output this without messing with a lot of files and – it’s simple.

Hope it helps someone like it did to me 🙂

<?php
$post = $wp_query->post;
$id = get_the_ID();
if ( class_exists( 'Lets_Review_API' ) ) {
$lets_review_api = new Lets_Review_API();
$lets_review_final_score = $lets_review_api->lets_review_get_final_score( $id );
echo $lets_review_final_score;
}
?>

Leave a Comment