Today, I decided to use this great donation WordPress plugin for our non-profit website (iraniansptgh.com). We are doing a fundraising to attend the Pittsburgh’s Bicentennial Anniversary Parade and since I’m maintaining the website I decided to quickly look into the existing WordPress plugins that I could find for donations and create a webpage for our fundraising.
I decided to finally use Charitable fundraising plugin, about which I have to say it is a GREAT plugin. Even the free version gives you tons of features you can use for creating multiple compaings or receiving online or offline donations through the website and etc. However, we decided to show the donors list on the website sorted by the donations amount (hiding the donation amount for sure) and it seems the website doesn’t support such a feature. It does have a nice widget but the widget has to be on a sidebar and sidebars are displayed in all pages!! Additionally it seemed to be impossible to hide the avatars which would make the list huge and large and mess up the pages’ design.
With all that said, I decided to create a short code for the plugin that lets you print the list of your donors any where you would like and gives you bunch of options including:
- Compaign ID
- Sort which can have the values [none, asc,desc]
- Sort Reference : the column value you would like to sort by that can have values [amount, first_name, last_name]
- Display amount: which shows whether you want to show the the donation amount or not
You can use the short code like:
[donors cid=123 sort=”none” sortref=”amount” showamount=false]
You can find the code for the shortcode on [github] or just below. If you are planning to use it easily add it to the end of the file “./includes/shortcodes/class-charitable-campaigns-shortcode.php” just before the “endif”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// function that is claled when short code is sued function function_donors( $atts ){ // setting the defautl values for paramters. Cid = 0 won't return any results $donors_atts = shortcode_atts( array( 'cid' => 0, 'sort' => 'none', 'sortref' => 'first_name', 'showamount' => false ), $atts ); // handling errro when cid is not provided if( $donors_atts['cid'==0]){ return "Error: Can't show donor list: cid must be provided in shortcode!!"; } global $wpdb; // creating query $query = ""; if(strcmp($donors_atts['sort'],'none')==1){ // if no sort, then randomize $query = "SELECT first_name, last_name, sum(amount) as amount FROM wp_charitable_campaign_donations as cd, wp_charitable_donors as d WHERE cd.donor_id = d.donor_id AND campaign_id = ".$donors_atts['cid']." GROUP by d.donor_id order by rand();"; }else{ // checking values for sort and sort ref to be valid if(!strcmp($donors_atts['sortref'],'amount') && !strcmp($donors_atts['sortref'],'first_name') && !strcmp($donors_atts['sortref'],'last_name')){ return "Error: Can't show donors list: wrong sortref value"; } if(!strcmp($donors_atts['sort'],'asc') && !strcmp($donors_atts['sort'],'desc')){ return "Error: Can't show donors list: wrong sort value"; } // creating query that sorts $query = "SELECT first_name, last_name, sum(amount) as amount FROM wp_charitable_campaign_donations as cd, wp_charitable_donors as d WHERE cd.donor_id = d.donor_id AND campaign_id = ".$donors_atts['cid']." GROUP by d.donor_id ORDER BY ".$donors_atts['sortref']." ".$donors_atts['sort'].";"; } // submiting query $mylink = $wpdb->get_results($query); // generating results as ul tag $tmp = "<ul class='donors'>"; foreach($mylink as $donor){ $fname = $donor->first_name; $lname = $donor->last_name; $sumamount =$donor->amount; //checking if amount of donations should be displayed if($donors_atts['showamount']==0){ $tmp = $tmp . "<li> ".$fname." ".$lname." </li>"; }else{ $tmp = $tmp . "<li> ".$fname." ".$lname.": ".$sumamount." </li>"; } } $tmp = $tmp . "</ul>"; return $tmp; } //addin shortcode add_shortcode( 'donors', 'function_donors' ); |
if you are wondering where to find cid, its the post id for your campaign.
2 comments On Display Donors List For Charitable WordPress Plugin
Thank you very much Salim, this was very useful to me.
Have a nice day,
Enrico
I’m happy to hear 🙂