Below, you’ll find a detailed guide on how to add the WP-PostRatings Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the WP-PostRatings Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the WP-PostRatings Plugin and the shortcodes it provides:
"WP-PostRatings is a dynamic WordPress plugin, allowing users to rate posts, thereby enhancing user engagement. The plugin slug is 'wp-postratings', facilitating easy installation."
- []
WP-PostRatings [ratings] Shortcode
The WP-PostRatings shortcode allows you to embed a rating system in your posts. It uses the shortcode: [ratings]. The shortcode function checks if the content is not a feed or AMP endpoint. If it’s not, it retrieves the post ID and decides whether to return the rating results or the rating itself. If the content is a feed or AMP endpoint, it displays a note to visit the post for rating.
Shortcode: [ratings]
Parameters
Here is a list of all possible null shortcode parameters and attributes:
id
– Specifies the unique identifier of the post to be rated.results
– If set to true, displays the rating results of the specified post.
Examples and Usage
Basic example – Displaying post ratings by referencing the post ID.
[ratings id=2 /]
Advanced examples
Displaying the rating results for a specific post, by setting the ‘results’ attribute to true. If the post is found, it will return the rating results, otherwise, it will return a message encouraging the user to rate the post.
[ratings id=3 results=true /]
Using the shortcode to display ratings without specifying a post ID. In this case, the shortcode will use the ID of the current post. If the post has not been rated, it will return a message encouraging the user to rate the post.
[ratings /]
Using the shortcode to display ratings for a post and hide the results. If the post is found, it will return the ratings without results, otherwise, it will return a message encouraging the user to rate the post.
[ratings id=4 results=false /]
PHP Function Code
In case you have difficulties debugging what causing issues with [null]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'ratings', 'ratings_shortcode' );
Shortcode PHP function:
function ratings_shortcode( $atts ) {
$attributes = shortcode_atts( array( 'id' => 0, 'results' => false ), $atts );
if( ! is_feed() && ! ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ) {
$id = (int)$attributes['id'];
if( $attributes['results'] ) {
return the_ratings_results( $id );
}
return the_ratings( 'span', $id, false );
}
return esc_html__( 'Note: There is a rating embedded within this post, please visit this post to rate it.', 'wp-postratings' );
}
Code file location:
wp-postratings/wp-postratings/includes/postratings-shortcodes.php
Conclusion
Now that you’ve learned how to embed the WP-PostRatings Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply