Expand filtering of results: Custom Image

Here is another post about expanding results displayed in widget with some elements that are not available from the widget settings panel. This time, here is the info on using image for post other than image from custom field or first image from content. Example will include use of featured image.

Throughout the GD Star Rating there are many, many filters you can use to expand the plugin. Easiest way to find them is by either looking at particular piece of code, or searching for a ‘apply_filters’ in the plugin code. These filters names usually start with ‘gdsr_’. This tutorial will show how to use any image for the post in the widget list.

Very Important: Making these kinds of changes is impossible without some knowledge of PHP/WordPress/SQL. Any complex filtering can’t be done without understanding how SQL works, how data is organized in WordPress. On it’s own plugin allows a lot of things, but it can do everything or we would have widget settings 5 pages long and hundreds of parameters in function calls. I am not going to explain how filters work in WordPress or how SQL works, that is already required knowledge for this.

This is quite simple thing to do. You only need to use one filter that will allow you to pull image for the post (or any results row in case of results grouping). Filter name is gdsr_widget_image_url_prepare. It gives 3 parameters. First one is actual URL to an image, second is the array with widget settings and third is row object with results.

Widget settings contain key image_from that can be ‘content’ or ‘custom’. First will tell plugin to find image in post, and second to use custom field. If it’s not set to either of these, image URL will be empty string by default. They can be set from widget control panel. But, in any case, you don’t need to use URL plugin gets from this key. Array with widget arguments can be used to identify the widget (if you have more than one on the page, or you need to identify some parameters in it like grouping or something else). Row with results contains all sorts of data that identify the post.

It would be hard to identify and explain all arguments in the arrays with widget settings and results, best way to see what’s in them is to print them on screen.

For this example we will use post thumbnail (or a featured image), and you need theme that supports it, and your posts need to have them set. But, same system can be used for getting images in any other way, so use this as a starting point. In your theme functions.php file, you can start by adding this:

[sourcecode language=”php”]<?php
add_filter("gdsr_widget_image_url_prepare", "gdsr_filter_widget_image_url", 10, 3);

function gdsr_filter_widget_image_url($url,$widget,$row) {
$url = get_the_post_thumbnail($row->post_id);
return $url;

That’s it! This will get thumbnail with default size and other parameters. Check out the get_the_post_thumbnail() function in WordPress Codex documentation for more details on what settings you can use. In the example function need at least post ID to find the thumbnail, and as I said, $row contains data about each post row with results, and in normal case with no grouping, $row represents one post.


Leave a Reply