Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example WordPress plugin to hook into the 'wpbcr_shortcode_best_comments_sql' filter of the WP-Comment Rating Plugin by WP-Buddy
<?php
/*
Plugin Name: My Shortcode Ranking Filter for WP-Comment Rating Plugin
Description: Hooks into the sql filter of the WP-Comment-Rating and allows the usage of the shortcode like this: [wpbcr_comment_ranking do="filter_by_xy_meta"]
*/
add_filter( 'wpbcr_shortcode_best_comments_sql', 'my_wpbcr_shortcode_best_comments_sql', 10, 6 );
/**
* The function called by the 'wpbcr_shortcode_best_comments_sql' filter and
* outputs a new SQL query depending on a comment meta value and meta key.
*
* Allows the usage of the shortcode like this: [wpbcr_comment_ranking do="filter_by_xy_meta"].
* Where the meta key is 'some_keyval' and the meta value is 'test' in this script.
*
* ATTENTION: This only works if the WP-Comment-Plugin version >= 1.2 is used.
*
* @param string $sql
* @param string $where
* @param string $sort_by
* @param int $limit
* @param int $post_id
* @param string $do
*
* @return string
*/
function my_wpbcr_shortcode_best_comments_sql( $sql, $where, $sort_by, $limit, $post_id, $do ) {
/*
* stop here if the 'do' variable is not 'filter_by_xy_meta'
*
* ATTENTION: replace the name 'filter_by_xy_meta' with your own value. (It is then used in the shortcode).
*/
if ( 'filter_by_xy_meta' != $do ) {
return $sql;
}
/**
* @var wpdb $wpdb
*/
global $wpdb;
// stop here if - in any case - the wpdb does not exist.
if ( ! is_a( $wpdb, 'wpdb' ) ) {
return $sql;
}
/*
* add a query to only return rows that have the 'meta_key' = 'some_value'
* where c is the original wp_comments table.
*
* ATTENTION: replace 'some_keyval' with your meta_key name.
*/
$query_add = ' LEFT JOIN ' . $wpdb->commentmeta . ' as my_q ON ( my_q.comment_ID = cm.comment_id AND my_q.meta_key = "some_keyval" )';
// hook in just before the WHERE clause.
$sql = str_replace( 'WHERE', $query_add . ' WHERE ', $sql );
/*
* hook into the WHERE clause.
*
* ATTENTION: replace 'test' with your own meta value.
*/
$sql = str_replace( 'WHERE', ' WHERE my_q.meta_value = "test" AND ', $sql );
return $sql;
}
@code-flow

This comment has been minimized.

Copy link
Owner Author

@code-flow code-flow commented Feb 13, 2014

Copy this into a file maybe called 'my-shortcode-ranking-filter.php' and put it into the /wp-contents/plugins/ folder of WordPress. Activate it from the administration panel. Use the shortcode like this:

[wpbcr_comment_ranking do="filter_by_xy_meta"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.