Last active
August 29, 2015 13:56
-
-
Save code-flow/8971156 to your computer and use it in GitHub Desktop.
Example WordPress plugin to hook into the 'wpbcr_shortcode_best_comments_sql' filter of the WP-Comment Rating Plugin by WP-Buddy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"]