Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save code-flow/9087421 to your computer and use it in GitHub Desktop.
Example WordPress plugin that shows the best comments based on the ratings for the WP-Comment Rating Plugin by WP-Buddy
<?php
/*
Plugin Name: My Shortcode Ranking Filter with comment template for WP-Comment Rating Plugin
Description: Displays the best comments with the template
*/
/*
* IMPORTANT: Only works for PHP >= 5.3 AND WP-Comment Rating >= 1.3
*/
add_action( 'init', 'my_add_shortcode_ranking_filter' );
function my_add_shortcode_ranking_filter() {
add_shortcode( 'my_shortcode_ranking', 'my_add_shortcode_ranking' );
}
// Only works since version 1.3
function my_add_shortcode_ranking( $atts, $content = '' ) {
$atts = shortcode_atts( array(
'post_id' => '',
'do' => 'best-rated',
'limit' => 5
), $atts );
/**
* @var wpbuddy\plugins\CommentRating\WPB_Comment_Rating $wpb_comment_rating
* @var WP_Post $post
* @var wpdb $wpdb
* @var WP_Query $wp_query
*/
global $wpb_comment_rating, $post, $wpdb, $my_shortcode_comments;
if ( ! is_a( $wpb_comment_rating, 'wpbuddy\plugins\CommentRating\WPB_Comment_Rating' ) ) {
return '';
}
if ( ! is_a( $post, 'WP_Post' ) ) {
return '';
}
$rows = $wpb_comment_rating->shortcode_comment_ranking( $atts, $content, 'rows' );
$ids = wp_list_pluck( $rows, 'comment_ID' );
if ( count( $ids ) <= 0 ) {
return '';
}
$sql = 'SELECT * FROM ' . $wpdb->comments . ' WHERE comment_ID IN(' . implode( ',', $ids ) . ') LIMIT ' . intval( $atts['limit'] );
$my_shortcode_comments = $wpdb->get_results( $sql );
add_filter( 'comments_array', 'my_comments_array_shortcode_filter', 10, 2 );
// This is using the comments template
ob_start();
comments_template( '/comments.php' );
remove_filter( 'comments_array', 'my_comments_array_shortcode_filter', 10 );
return ob_get_clean();
/*
* This is using wp_list_comments(). Should be called like the one in your comments.php
*
ob_start();
wp_list_comments( array(
'style' => 'ol',
'short_ping' => true,
'avatar_size'=> 34,
), $comments );
return ob_get_clean();
*/
}
function my_comments_array_shortcode_filter( $comments, $post_id ) {
global $my_shortcode_comments;
return $my_shortcode_comments;
}
@code-flow
Copy link
Author

Please note that this only works for PHP >= 5.3 and WP-Comment Rating >= 1.3.

Please also note that comments_template() will only work if the shortcode is placed in a page or a post. It will not work in a sidebar for example.

You have to copy & paste the source-code into a file maybe called 'my-shortcode-ranking-filter-comment-template.php' and upload it to your /wp-contents/plugins/ folder. Then activate it right from the "Plugin" menu in your WordPress installation.

After that you can access the shortcode by using this in the editor (example):

[my_shortcode_ranking do="best-rated" limit="7"]

You can use the same attributes as described in the FAQ (question 6): http://wp-buddy.com/documentation/plugins/wordpress-comment-rating-plugin/faq/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment